gpt4 book ai didi

ios - View 层次结构没有为约束准备?

转载 作者:行者123 更新时间:2023-11-29 12:16:05 25 4
gpt4 key购买 nike

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

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。

关于ios - View 层次结构没有为约束准备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31974174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com