Skip to content

Instantly share code, notes, and snippets.

@mayoff
Created June 14, 2013 23:53
Show Gist options
  • Save mayoff/5786135 to your computer and use it in GitHub Desktop.
Save mayoff/5786135 to your computer and use it in GitHub Desktop.
Example of using auto layout to make a container view as tall as its tallest subview, but no taller. For http://stackoverflow.com/q/17117799/77567 .
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController {
UIView *containerView;
NSArray *subviews;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.translatesAutoresizingMaskIntoConstraints = NO;
[self makeContainerView];
[self makeSubviews];
}
- (void)makeContainerView {
containerView = [[UIView alloc] init];
containerView.translatesAutoresizingMaskIntoConstraints = NO;
containerView.backgroundColor = [UIColor colorWithHue:0.2 saturation:0.1 brightness:0.9 alpha:1];
[self.view addSubview:containerView];
// Horizontal constraints
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[containerView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(containerView)]];
// Vertically center the container view in my view
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:0.5 constant:0]];
// Ask the container to have height 0, but give the constraint a low priority.
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:0 constant:0];
constraint.priority = UILayoutPriorityDefaultLow;
[containerView addConstraint:constraint];
}
- (void)makeSubviews {
UIView *subview0 = [self makeSubviewWithFrame:CGRectMake(20, 0, 80, 50)];
UIView *subview1 = [self makeSubviewWithFrame:CGRectMake(120, 0, 80, 100)];
UIView *subview2 = [self makeSubviewWithFrame:CGRectMake(220, 0, 80, 80)];
subviews = @[ subview0, subview1, subview2 ];
}
- (UIView *)makeSubviewWithFrame:(CGRect)frame {
UIView *subview = [[UIView alloc] init];
subview.translatesAutoresizingMaskIntoConstraints = NO;
subview.backgroundColor = [UIColor colorWithHue:0.5 saturation:0.1 brightness:0.9 alpha:1];
[containerView addSubview:subview];
// Set the subview's size and X position from the frame. The Y position will be determined by auto layout.
[subview addConstraint:[NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:0 constant:frame.size.width]];
[subview addConstraint:[NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:0 constant:frame.size.height]];
[containerView addConstraint:[NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:containerView attribute:NSLayoutAttributeLeading multiplier:1 constant:frame.origin.x]];
// Pin the bottom of subview to the bottom of containerView.
[containerView addConstraint:[NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:containerView attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
// Constrain the container view to be at least as tall as the subview.
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:subview attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
constraint.priority = UILayoutPriorityRequired;
[containerView addConstraint:constraint];
return subview;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment