【发布时间】:2013-06-12 21:41:09
【问题描述】:
我的目标:将指示器(垂直和水平)居中到视图中的按钮
我正在做的是:
[self.logInButton addConstraint:[NSLayoutConstraint constraintWithItem:spinner1
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.logInButton
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0]];
[self.logInButton addConstraint:[NSLayoutConstraint constraintWithItem:spinner1
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.logInButton
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0]];
self.logInButton addSubView:spinner
但是,我在下面收到警告
The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x17e19390 UIActivityIndicatorView:0x17d035d0.centerY == UIButton:0x17df4430.centerY>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2013-06-12 17:37:19.610 FlipGive[366:60b] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x17e19810 UIActivityIndicatorView:0x17d035d0.centerX == UIButton:0x17df4430.centerX>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
有谁知道我在这里做错了什么?请帮忙
这里欢迎所有的 cmets。谢谢
【问题讨论】:
-
将约束添加到按钮和微调器的父视图中。
-
您的评论有什么解释吗?我只是想了解deely
-
约束引用的所有视图都必须在它被添加到的视图的子树中。通常,将约束添加到最近的祖先以获得更好的性能,或者添加到它在逻辑上属于的组件(在本例中为登录按钮)。
标签: ios objective-c autolayout nslayoutconstraint