使用 iOS 自动布局在子视图控制器与其父视图之间建立约束
Posted
技术标签:
【中文标题】使用 iOS 自动布局在子视图控制器与其父视图之间建立约束【英文标题】:Using iOS Auto Layout to establish constraints between a child view controller and its parent view 【发布时间】:2012-11-02 17:47:06 【问题描述】:在我的根视图控制器中,我将子视图控制器的视图添加为子视图,如下所示:
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 介绍视频的自动布局),所以我设置了[childview setTranslatesAutoresizingMaskIntoConstraints:NO]
,但随后子视图根本没有出现。
我做错了什么?
【问题讨论】:
【参考方案1】:我不确定,但以下应该可以工作,或者非常相似:
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 宽度的子视图,因为这会留下默认边距),但它只是一个例子。
【讨论】:
【参考方案2】:要使用自动布局正确设置和调整子视图控制器的大小,请执行以下操作:
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]];
【讨论】:
以上是关于使用 iOS 自动布局在子视图控制器与其父视图之间建立约束的主要内容,如果未能解决你的问题,请参考以下文章
iOS:在 iOS 7 和 iOS 8 上将约束应用于子视图的自动布局差异