使用iOS自动布局在子视图控制器及其父视图之间建立约束

时间:2012-11-02 17:47:07

标签: ios uiviewcontroller autolayout

在我的根视图控制器中,我将子视图控制器的视图添加为子视图,如下所示:

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

我现在想使用NSLayoutConstraint在父视图(self.view)中定位cvc.view,这样cvc.view定位在父视图底部上方25个点。我的理解是以下应该有效:

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];

但约束在运行时失败。我认为最初可能是子视图中的自动调整掩码导致了问题(并且在自动布局上遵循WWDC 2012简介视频),因此我设置了[childview setTranslatesAutoresizingMaskIntoConstraints:NO],但是子视图根本无法显示。

我做错了什么?

2 个答案:

答案 0 :(得分:3)

我不确定,但以下内容应该有效,或者非常类似:

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];

如果不确定您实际上是为子视图设置了大小,请执行以下操作:

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];

说,让它填充视图的宽度。或者:

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];

这意味着像childview的宽度应该大于100但小于304,并且superview的默认边距。请注意我不知道上述约束是否真的有意义(例如它可能总是给你304宽度子视图,因为它会留下默认边距),但它就是一个例子。

答案 1 :(得分:0)

要使用自动布局正确设置和调整子视图控制器的大小,请执行以下操作:

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];

如果您更喜欢视觉格式,可以执行以下操作:

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]];