[英]iOS View Hierarchy for setting up Auto Layout Constraint
What is the closest ancestor to self.navigationController.navigationBar
and a subview of self.view
in a view controller so I can add the constraint to that ancestor? 什么是最接近的祖先self.navigationController.navigationBar
和一个子视图self.view
在一个视图控制器,所以我可以约束添加到祖先?
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[self.searchBar setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.searchBar];
NSLayoutConstraint* cn = [NSLayoutConstraint constraintWithItem:self.searchBar
attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual
toItem:self.navigationController.navigationBar attribute:NSLayoutAttributeBottom
multiplier:1.0 constant:0];
I tried doing 我试着做
[self.view addConstraint:cn];
[self.navigationController.view addConstraint:cn];
but both times I get the error, "Does the constraint reference something from outside the subtree of the view? That's illegal." 但是两次都出现错误,“约束是否从视图的子树外部引用了某些东西?这是非法的。”
Can someone explain the view hierarchy of a view controller in relation to the navigationController? 有人可以解释相对于navigationController的视图控制器的视图层次结构吗?
Thanks! 谢谢!
The view tree looks like this: 视图树如下所示:
So, navigationController.view is the closest ancestor of the navigation bar and your view. 因此,navigationController.view是导航栏和您的视图的最接近的祖先。 I'm not sure why that didn't work. 我不确定为什么那没用。 Where did you put that code? 您将代码放在哪里?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.