7

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

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

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

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 Intro video on auto layout),所以我设置[childview setTranslatesAutoresizingMaskIntoConstraints:NO]了 ,但随后子视图根本无法出现。

我究竟做错了什么?

4

2 回答 2

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 宽度的子视图,因为这会留下默认边距),但它只是一个例子。

于 2012-11-29T17:31:31.397 回答
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]];
于 2014-09-22T16:48:37.297 回答