视图层次结构没有为约束准备?

The view hierarchy is not prepared for the constraint?

我收到以下错误,即使我在添加按钮和应用约束之前设置了 table 视图页脚。在 -viewDidLoad-viewDidAppear 中设置和页脚视图后,我尝试调用以下方法。我似乎无法隔离问题。

The view hierarchy is not prepared for the constraint: 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.

2015-08-12 15:43:05.086 myApp[2293:299005] View hierarchy unprepared for constraint. Constraint: Container hierarchy: > | > View not found in container hierarchy: > That view's superview: ; layer = ; contentOffset: {0, 0}; contentSize: {800, 75}>

这是我用来添加按钮和应用约束的代码:

- (void)addButtonContraints{
//Create button
    _nextQuestionButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_nextQuestionButton addTarget:self action:@selector(nextQuestion) forControlEvents:UIControlEventTouchUpInside];
    //_nextQuestionButton.frame = CGRectMake(0, 0, 200,40);
    [_nextQuestionButton setTitle:@"Next Question" forState:UIControlStateNormal];
    [_nextQuestionButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _nextQuestionButton.backgroundColor = [UIColor whiteColor];
    _nextQuestionButton.titleLabel.attributedText = [[NSAttributedString alloc] initWithString:@"Next Question"
                                                                                    attributes:@{NSForegroundColorAttributeName:[UIColor blackColor], NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0f]}];

    //Add shadow to button
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:_nextQuestionButton.bounds];
    _nextQuestionButton.layer.masksToBounds = NO;
    _nextQuestionButton.layer.shadowColor = [UIColor blackColor].CGColor;
    _nextQuestionButton.layer.shadowOffset = CGSizeMake(0.0f, 5.0f);
    _nextQuestionButton.layer.shadowOpacity = 0.25f;
    _nextQuestionButton.layer.shadowPath = shadowPath.CGPath;

    [_nextQuestionButton setTranslatesAutoresizingMaskIntoConstraints:NO];


    [self.quizTableView.tableFooterView addSubview:_nextQuestionButton];



    //Add contraints to button

    NSDictionary *dict = NSDictionaryOfVariableBindings(_nextQuestionButton);

    //width
    [_nextQuestionButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_nextQuestionButton(200)]"
                                                                                options:0x00
                                                                                metrics:nil
                                                                                  views:dict]];
    //height
    [_nextQuestionButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_nextQuestionButton(40)]"
                                                                                options:0x00
                                                                                metrics:nil
                                                                                  views:dict]];
    //right
    [_nextQuestionButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[_nextQuestionButton]-40-|"
                                                                                options:0x00
                                                                                metrics:nil
                                                                                  views:dict]];
    //top
    [_nextQuestionButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[_nextQuestionButton]"
                                                                                options:0x00
                                                                                metrics:nil
                                                                                  views:dict]];
    _nextQuestionButton.hidden = YES;


}

您应该将最后两个约束添加到您的 self.quizTableView.tableFooterView,而不是 _nextQuestionButton。