【发布时间】: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],但随后子视图根本没有出现。
我做错了什么?
【问题讨论】:
标签: ios uiviewcontroller autolayout