YIVariableViewSize 1.0.2

YIVariableViewSize 1.0.2

TestsTested
LangLanguage Obj-CObjective C
License Custom
ReleasedLast Release Dec 2014

Maintained by Unclaimed.


Downloads

Total300
Week0
Month0

Installs

Apps4
Test Targets1
powered by Segment

GitHub

Stars11
Watchers4
Forks1
Issues0
Contributors1
Pull Requests0

Code

Files4
LOCLines of Code 117


 
Depends on:
JRSwizzle~> 1.0
ViewUtils~> 1.1
 


  • By
  • Yasuhiro Inami

YIVariableViewSize 1.0.2

Layout subviews first, then its container. Not using AutoLayout, works on iOS5+.

(developed in #objectivechackathon Put Objective C Back On The Map)

ScreenShot1

How to use

YIVariableViewSize is actually an abstract concept written as ObjC-protocol style. Thus, your job is to conform your UIView subclass to <YIVariableViewSize> protocol and implement its methods something like this:

- (CGSize)sizeThatFits:(CGSize)size
{
    [self resizeSubviewsWithFittingSize:size];
    [self positionSubviews];

    size.height = self.mostBottomVisibleSubview.bottom + self.paddingBottomForVariableViewSize;

    return size;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self positionSubviews];  // don't resize, just set origin
}

// change subview's frame.size & drawing contents
- (void)resizeSubviewsWithFittingSize:(CGSize)fittingSize
{
    [self.subview1 sizeToFitWithMaxSize:fittingSize];
    [self.subview2 sizeToFitWithMaxSize:fittingSize];
}

// change subview's frame.origin
- (void)positionSubviews
{
    self.subview2.top = self.subview1.bottom;
}

- (CGFloat)paddingBottomForVariableViewSize
{
    return 10;
}

Notice that -sizeThatFits: actually calculates subview's size, position, and lastly paddingBottom(Right), where -layoutSubviews will only handle their positioning. By doing so, we can split the resizing and positioning code, and never try to resize subview's content on -layoutSubviews, which is performance-critical.

So far so good. But you can even omit writing -sizeThatFits: and -layoutSubviews if you call [YIVariableViewSize install] in your AppDelegate.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [YIVariableViewSize install];
    return YES;
}

Dependencies

License

YIVariableViewSize is available under the Beerware license.

If we meet some day, and you think this stuff is worth it, you can buy me a beer in return.