ios - 在自动布局中将 subview 的 X 居中抛出 "not prepared for t

我有一个自定义 UIView 子类,它正在通过 nib 初始化。

-awakeFromNib 中,我正在创建一个 subview 并尝试将其置于其父 View 的中心。

[self setInteralView: [[UIView alloc] init]];
[[self internalView] addConstraint: [NSLayoutConstraint constraintWithItem: [self internalView]
                                                                 attribute: NSLayoutAttributeCenterX
                                                                 relatedBy: NSLayoutRelationEqual
                                                                    toItem: self
                                                                 attribute: NSLayoutAttributeCenterX
                                                                multiplier: 1
                                                                  constant: 0]];

这会中断,并导致以下输出:

2013-08-11 17:58:29.628 MyApp[32414:a0b] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0xc1dcc80 UIView:0xc132a40.centerX == MyView:0xc1315a0.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.
2013-08-11 17:58:29.630 MyApp[32414:a0b] View hierarchy unprepared for constraint.
    Constraint: <NSLayoutConstraint:0xc1dcc80 UIView:0xc132a40.centerX == MyView:0xc1315a0.centerX>
    Container hierarchy: 
<UIView: 0xc132a40; frame = (0 0; 0 0); clipsToBounds = YES; layer = <CALayer: 0xc132bc0>>
    View not found in container hierarchy: <MyView: 0xc1315a0; frame = (-128 -118; 576 804); layer = <CALayer: 0xc131710>>
    That view's superview: <UIView: 0xc131a70; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0xc131b50>>

最佳答案

对于来到这里的其他人:我收到此错误,因为我在将 subview 添加到层​​次结构之前添加了约束。

关于ios - 在自动布局中将 subview 的 X 居中抛出 "not prepared for the constraint",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18177262/

相关文章:

objective-c - 如何使用 AFNetworking 设置超时

objective-c - Xcode 不断要求输入密码以使用系统钥匙串(keychain)

ios - UITableView 中的圆形 UIImageView 没有性能影响?

objective-c - 在 Objective-C 中将所有文本转换为小写

objective-c - 我不想在开始更新、结束更新 block 中为 uitableview 设

ios - 在 SKScene 中设置按钮

objective-c - sizeWithFont 方法已弃用。 boundingRectWith

objective-c - 值/对象的 NSDictionary 键?

ios - AFNetworking 发布请求

objective-c - 基于 View 的 NSTableView 具有动态高度的行