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

标签 ios autolayout

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

- (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但没有帮助

最佳答案

替换这个

[labe2 addConstraint:yConstraint];

[self.view addConstraint:yConstraint];

关于ios - 添加 Y 约束时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34328992/

相关文章:

objective-c - 改变正在进行的 CAKeyframeAnimation 动画的速度

ios - UITableViewController 表删除后不会刷新,但会在 viewWillAppear 上刷新

ios - UITableView - 如何动态更新自调整大小单元格的框架

ios - 在 viewDidLayoutSubviews 中找不到约束 - AutoLayout

ios - 表头 View 自动布局问题

ios - 我可以在 NSURLSession 创建后更新其配置中的 cookie 吗?

ios - 错误的 iOS8 状态栏?

ios - 调整UITextView的大小不遵守约束

ios - 静态 UITableViewCell 的 contentView 中带有 UITextField 的模糊布局

ios - 如何使 UITableViewCell 居中,约束无法对齐