在我的根视图控制器中,我将一个子视图控制器的视图添加为子视图,如下所示:
ChildViewController *cvc = [[ChildViewController alloc] init];
[self addChildViewController:cvc];
[self.view addSubview:cvc.view];
[cvc didMoveToParentViewController:self];
我现在希望使用NSLayoutConstraint将cvc.view定位到父视图(self.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]
,但是子视图就无法显示。
我做错了什么?