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

时间:2015-08-12 19:51:15

标签: ios nslayoutconstraint

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

  

未为约束准备视图层次结构:     添加到。时   在视图中,约束的项必须是该视图的后代(或者   查看自己)。如果需要解决约束,这将崩溃   在组装视图层次结构之前。打破 - [UIView   _viewHierarchyUnpreparedForConstraint:]进行调试。

     

2015-08-12 15:43:05.086 myApp [2293:299005]查看层次结构未准备好   为了约束。约束:容器层次结构:> |   >在容器层次结构中找不到视图:   >那个观点的超级观点:; 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;


}

1 个答案:

答案 0 :(得分:1)

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