在自动布局子视图围绕公司的X抛出"未prepared的约束" [英] Centering subview's X in autolayout throws "not prepared for the constraint"

查看:182
本文介绍了在自动布局子视图围绕公司的X抛出"未prepared的约束"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在通过笔尖初始化一个自定义UIView子类。

-awakeFromNib ,我创建一个子视图,并试图居中于它的父。

  [自setInteralView:[​​UIView的页头]初始化];
[自internalView] addConstraint:[NSLayoutConstraint constraintWithItem:[个体经营internalView]
                                                                 属性:NSLayoutAttributeCenterX
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:自我
                                                                 属性:NSLayoutAttributeCenterX
                                                                事半功倍:1
                                                                  常数:0]];

这打破,并导致下面的输出:

  2013年8月11日17:58:29.628的MyApp [32414:A0B]视图层次不是prepared的约束:< NSLayoutConstraint:0xc1dcc80的UIView:0xc132a40。的centerX == MyView的:0xc1315a0.centerX>
    当添加到视图,约束的物品必须是视图(或视图本身)的后裔。如果约束所需​​要的视图层次组装之前解决这将崩溃。打破 - [UIView的_viewHierarchyUn preparedForConstraint:]调试。
2013年8月11日17:58:29.630的MyApp [32414:A0B]查看ppared的层次约束未$ P $。
    约束:< NSLayoutConstraint:0xc1dcc80的UIView:0xc132a40.centerX == MyView的:0xc1315a0.centerX>
    容器层次:
<的UIView:0xc132a40;帧=(0 0 0 0); clipsToBounds = YES;层= LT; CALayer的:0xc132bc0>>
    图中未在容器层次结构中发现:其中,MyView的:0xc1315a0;帧=(-128 -118 576 804);层= LT; CALayer的:0xc131710>>
    这一观点的上海华:<的UIView:0xc131a70;帧=(0 0 320 568); AUTORESIZE = W + H;层= LT; CALayer的:0xc131b50>>


解决方案

由于错误状态,则必须将约束添加到一个视图,使得参与约束的看法是你将它添加到或子视图的视图的这一观点。在你上面的code的情况下,你应该补充说,制约,而不是 [自internalView] 。书面它不工作的原因是参与约束一个视图()是不是在视图层次,如果你只考虑 [个体经营internalView] 以下)。

有关详细信息,请参阅<一个href=\"http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#jumpTo_51\">discussion文档的部分上的 addConstraint 的:

下面是你的code线,固定为我建议:

  UIView的* internalView = [[UIView的页头] initWithFrame:方法CGRectZero];
internalView.translatesAutoresizingMaskIntoConstraints = NO;
[个体经营setInternalView:internalView];[个体经营addConstraint:[NSLayoutConstraint constraintWithItem:[个体经营internalMapView]
                                         属性:NSLayoutAttributeCenterX
                                         relatedBy:NSLayoutRelationEqual
                                         toItem:自我
                                         属性:NSLayoutAttributeCenterX
                                         事半功倍:1
                                         常数:0]];

I have a custom UIView subclass which is being initialized via a nib.

In -awakeFromNib, I'm creating a subview and attempting to center it in its superview.

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

This breaks, and causes the following output:

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>>

解决方案

As the error states, you must add the constraint to a view such that the views involved in the constraint are the view you're adding it to or a subview of that view. In the case of your code above, you should be adding that constraint to self, rather than [self internalView]. The reason it doesn't work as written is one of the views involved in the constraint (self) is not in the view hierarchy if you consider only [self internalView] and below).

For more details, see the discussion section of the documentation on the addConstraint: method.

Here is your line of code, fixed as I suggest:

UIView* internalView = [[UIView alloc] initWithFrame:CGRectZero];
internalView.translatesAutoresizingMaskIntoConstraints = NO;
[self setInternalView:internalView];

[self addConstraint: [NSLayoutConstraint constraintWithItem: [self internalMapView]
                                         attribute: NSLayoutAttributeCenterX
                                         relatedBy: NSLayoutRelationEqual
                                         toItem: self
                                         attribute: NSLayoutAttributeCenterX
                                         multiplier: 1
                                         constant: 0]];

这篇关于在自动布局子视图围绕公司的X抛出&QUOT;未prepared的约束&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆