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

Posted

技术标签:

【中文标题】添加 Y 约束时应用程序崩溃【英文标题】:Application crashes when adding a Y constraint 【发布时间】:2015-12-17 07:33:12 【问题描述】:

我的代码很简单。我需要一个接一个地堆叠我的视图。在 self.view 中添加两个视图,空间为 20 像素

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

请帮忙。罢工了几个小时 通过this 但没有帮助

【问题讨论】:

【参考方案1】:

替换这个

[labe2 addConstraint:yConstraint];

[self.view addConstraint:yConstraint];

【讨论】:

为什么要superview?当关联 2 个视图时? 这样superview应该知道如何布局subview(label2)。

以上是关于添加 Y 约束时应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

添加约束会使应用程序崩溃

在视图之间转换时应用程序有时会崩溃:如何调试约束?

以编程方式添加约束会导致崩溃

iOS 8 Xcode 6:使用顶部/底部布局指南进行顶部/底部约束时,应用程序始终崩溃

当 AutoLayout 在调试控制台中记录无法满足的约束时,如何使我的应用程序崩溃?

由于 heightConstraints 我的应用程序崩溃了我不知道如何解决它