Autolayout约束视图层次结构错误

时间:2017-12-19 23:19:53

标签: ios autolayout

在代码中创建一个tableview后,我试图创建一个约束,设置新的tableview和Storyboard中存在的textview上面的textview之间的距离。我可以在tableview和superview之间设置约束,但不能在tableview和容器中的其他元素之间设置约束。错误是:

The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x600000c9be90 UITableView:0x7fe0cb126600.top == UITextView:0x7fe0ca128600.top + 12   (inactive)>
    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.  Would appreciate any suggestions on what to try.

这就是我创建约束的方式。

   _autocompleteTableView.translatesAutoresizingMaskIntoConstraints = NO;

    NSLayoutConstraint *leadingTableConstraint = [NSLayoutConstraint
                                                 constraintWithItem:self.autocompleteTableView attribute:NSLayoutAttributeLeft
                                                 relatedBy:NSLayoutRelationEqual toItem:self.view attribute:
                                                 NSLayoutAttributeLeft multiplier:1.0 constant:20];

    NSLayoutConstraint *trailingTableConstraint = [NSLayoutConstraint
                                                  constraintWithItem:self.view attribute:NSLayoutAttributeRight
                                                  relatedBy:NSLayoutRelationEqual toItem:self.autocompleteTableView attribute:
                                                  NSLayoutAttributeLeft multiplier:1.0 constant:20];

    NSLayoutConstraint *topTableConstraint = [NSLayoutConstraint
                                                 constraintWithItem:self.autocompleteTableView attribute:NSLayoutAttributeTop
                                                 relatedBy:NSLayoutRelationEqual toItem:self.subField attribute:
                                                 NSLayoutAttributeTop multiplier:1.0f constant:12];


    NSLayoutConstraint *heightTableConstraint = [NSLayoutConstraint constraintWithItem:self.autocompleteTableView
                                                                        attribute:NSLayoutAttributeHeight
                                                                        relatedBy:NSLayoutRelationEqual
                                                                           toItem:nil
                                                                        attribute:NSLayoutAttributeNotAnAttribute
                                                                       multiplier:1.0
                                                                         constant:300];


    [self.view addConstraints:@[leadingTableConstraint, trailingTableConstraint]];

     [self.autocompleteTableView addConstraints:@[heightTableConstraint]];
 //CRASHES ON NEXT LINE   
    [self.subField addConstraints:@[topTableConstraint]];

编辑:

我通过在self.view的最后一行调用addConstraints而不是self.subfield来防止崩溃:

[self.view addConstraints:@ [topTableConstraint]]; //而不是上面的最后一行

现在代码不再抛出异常,tableview根本没有出现。

0 个答案:

没有答案