简体   繁体   中英

The view hierarchy is not prepared for the constraint?

I am receiving the following error, even though I have set the table view footer before adding the button and applying the constraints. I have tried calling the below method after setting and footer view in -viewDidLoad and also in -viewDidAppear . I cannot seem to isolate the problem.

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

Here is the code that I am using to add the button and apply the constraints:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM