添加Y约束时应用程序崩溃

时间:2015-12-17 07:33:13

标签: ios autolayout

我的代码非常简单。我需要一个接一个地堆叠我的视图。在self.view中添加20 px空间的两个视图

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *label = [[UILabel alloc] init];
    [label setTranslatesAutoresizingMaskIntoConstraints:NO];
    [label.layer setBorderWidth:1.f];
    [label.layer setBorderColor:[[UIColor grayColor] CGColor]];
    [label setText:@"label1"];
    [self.view addSubview:label];
    NSLayoutConstraint *yConstraint =[NSLayoutConstraint
                                      constraintWithItem:label
                                      attribute:NSLayoutAttributeTop
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:label.superview
                                      attribute:NSLayoutAttributeTop
                                      multiplier:1.f
                                      constant:20];
    [label.superview addConstraint:yConstraint];
    NSLayoutConstraint *width =[NSLayoutConstraint
                                constraintWithItem:label
                                attribute:NSLayoutAttributeWidth
                                relatedBy:NSLayoutRelationEqual
                                toItem:label.superview
                                attribute:NSLayoutAttributeWidth
                                multiplier:1.f
                                constant:0];
    [label.superview addConstraint:width];
    NSLayoutConstraint *centerX =[NSLayoutConstraint
                                  constraintWithItem:label
                                  attribute:NSLayoutAttributeCenterX
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:label.superview
                                  attribute:NSLayoutAttributeCenterX
                                  multiplier:1.0
                                  constant:0];
    [label.superview addConstraint:centerX];

    NSLayoutConstraint *height =[NSLayoutConstraint
                                 constraintWithItem:label
                                 attribute:NSLayoutAttributeHeight
                                 relatedBy:NSLayoutRelationEqual
                                 toItem:nil
                                 attribute:NSLayoutAttributeNotAnAttribute
                                 multiplier:1.0
                                 constant:40];
    [label addConstraint:height];

    UILabel *labe2 = [[UILabel alloc] init];
    [labe2 setTranslatesAutoresizingMaskIntoConstraints:NO];
    [labe2 setText:@"label2"];
    [self.view addSubview:labe2];

    yConstraint =[NSLayoutConstraint
                                      constraintWithItem:labe2
                                      attribute:NSLayoutAttributeTop
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:label
                                      attribute:NSLayoutAttributeBottom
                                      multiplier:1.f
                                      constant:20];
    [labe2 addConstraint:yConstraint]; //Crashes here 


}

留言

2015-12-17 12:59:40.313 Testcase[16625:14518988] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7ae4f990 V:[UILabel:0x7ae4ac30'label1']-(20)-[UILabel:0x7ae4f610'label2']>
    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(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug.
2015-12-17 12:59:40.315 Testcase[16625:14518988] View hierarchy unprepared for constraint.
    Constraint: <NSLayoutConstraint:0x7ae4f990 V:[UILabel:0x7ae4ac30'label1']-(20)-[UILabel:0x7ae4f610'label2']>
    Container hierarchy: 
<UILabel: 0x7ae4f610; frame = (0 0; 0 0); text = 'label2'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7ae4f720>>
    View not found in container hierarchy: <UILabel: 0x7ae4ac30; frame = (0 0; 0 0); text = 'label1'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7ae4c3e0>>
    That view's superview: <UIView: 0x7ae4af30; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x7ae4a610>>

Plz帮助。敲了几个小时  通过this但没有帮助

1 个答案:

答案 0 :(得分:2)

替换此

[labe2 addConstraint:yConstraint];

[self.view addConstraint:yConstraint];