Using iOS Auto Layout to set limits between a child view controller and its parent view

In my root view controller, I add the child view controller view as a subview, as shown below:

ChildViewController *cvc = [[ChildViewController alloc] init]; [self addChildViewController:cvc]; [self.view addSubview:cvc.view]; [cvc didMoveToParentViewController:self]; 

and now I would like to use NSLayoutConstraint to place cvc.view in the parent view (self.view), so cvc.view positions 25 pts above the bottom of the parent view. I understand that the following should work:

 UIView *superview = self.view; UIView *childview = cvc.view; NSLayoutConstraint *cn = [NSLayoutConstraint withItem:childview attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeBottom multiplier: 1.0 constant: -25.0]; [superview addConstraint: cn]; 

But the restriction is not met at run time. At first I thought that maybe the autoresist mask in the child view was causing problems (and after the WWDC 2012 video in the automatic layout), so I set [childview setTranslatesAutoresizingMaskIntoConstraints:NO] , but then the child view just does not appear.

What am I doing wrong?

+7
source share
2 answers

I'm not sure, but the following should work, or something very similar:

 UIView *superview = self.view; UIView *childview = cvc.view; NSDictionary *constrainedViews = NSDictionaryOfVariableBindings(childview); NSArray *constraints = [superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[childview]-25-|" options:0 metrics:nil views:constrainedViews]; 

If you are not convinced that you are actually setting the size for the childview, follow these steps:

 UIView *superview = self.view; UIView *childview = cvc.view; NSDictionary *constrainedViews = NSDictionaryOfVariableBindings(childview); NSArray *constraints = [superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[childview]|" options:0 metrics:nil views:constrainedViews]; 

Say it fills the width of the view. Or:

 UIView *superview = self.view; UIView *childview = cvc.view; NSDictionary *constrainedViews = NSDictionaryOfVariableBindings(childview); NSArray *constraints = [superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[childview(>100,<304)]-|" options:0 metrics:nil views:constrainedViews]; 

This means that something like childview should have a width greater than 100, but less than 304 with default fields for the supervisor. Please note that I don’t know whether the specified restriction really makes sense (for example, it can always give you a 304-width childview, as this will leave the fields default), but this serves as an example.

+3
source

To properly configure and adjust the size of your child view controller using the automatic layout, follow these steps:

 childView.translatesAutoresizingMaskIntoConstraints = NO; NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:superview attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:childview attribute:NSLayoutAttributeLeft multiplier:1 constant:0]; [self.view addConstraint:left]; NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:superview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:childview attribute:NSLayoutAttributeTop multiplier:1 constant:0]; [self.view addConstraint:top]; NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:superview attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:childview attribute:NSLayoutAttributeWidth multiplier:1 constant:0]; [self.view addConstraint:width]; NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:superview attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:childview attribute:NSLayoutAttributeHeight multiplier:1 constant:0]; [self.view addConstraint:height]; 

If you prefer a visual format, you can do the following:

 childView.translatesAutoresizingMaskIntoConstraints = NO; NSDictionary *views = @{@"childview": childview}; [superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-0-[childview]-0-|" options:0 metrics:nil views:views]]; [superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[childview]-0-|" options:0 metrics:nil views:views]]; 
0
source

All Articles