添加约束使应用程序崩溃
Posted
技术标签:
【中文标题】添加约束使应用程序崩溃【英文标题】:Adding constraint crash down the application 【发布时间】:2015-06-13 07:33:20 【问题描述】:我有一个用于设计 UITableViewCell 的自定义 UIView,每件事都能完美运行,只是没有正确调整大小。当我尝试添加约束时它崩溃了。我使用了下面的代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
if(!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];
UIView *vBackView = [[UIView alloc] initWithFrame:aFrame];
[[cell contentView] addSubview:vBackView];
[vBackView addConstraint:[NSLayoutConstraint constraintWithItem:[cell contentView]
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:vBackView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0]];
[vBackView addConstraint:[NSLayoutConstraint constraintWithItem:[cell contentView]
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:vBackView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0]];
[vBackView addConstraint:[NSLayoutConstraint constraintWithItem:[cell contentView]
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:vBackView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0]];
[vBackView addConstraint:[NSLayoutConstraint constraintWithItem:[cell contentView]
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:vBackView
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0]];
// Doing other stuff
return cell;
在日志中显示
2015-06-13 12:51:57.482 DigitalBoard[5314:70921] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7fb508dc0ae0 UITableViewCell:0x7fb508dbb0a0'cellIdentifier'.top == UIView:0x7fb508dc16f0.top>
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-06-13 12:51:57.485 DigitalBoard[5314:70921] View hierarchy unprepared for constraint.
Constraint: <NSLayoutConstraint:0x7fb508dc0ae0 UITableViewCell:0x7fb508dbb0a0'cellIdentifier'.top == UIView:0x7fb508dc16f0.top>
Container hierarchy:
<UIView: 0x7fb508dc16f0; frame = (0 0; 400 44); autoresize = RM+BM; tag = 1; layer = <CALayer: 0x7fb508dc0720>>
| <UILabel: 0x7fb508dc5310; frame = (15 0; 105 44); text = 'Meeting #'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; tag = 2; layer = <_UILabelLayer: 0x7fb508dc54c0>>
| <UIButton: 0x7fb508dc5880; frame = (130 7; 80 30); opaque = NO; autoresize = RM+BM; tag = 3; layer = <CALayer: 0x7fb508da95d0>>
| <UIButton: 0x7fb508dc5fa0; frame = (220 7; 80 30); opaque = NO; autoresize = RM+BM; tag = 4; layer = <CALayer: 0x7fb508dc5c60>>
| <UIButton: 0x7fb508dc61c0; frame = (310 7; 80 30); opaque = NO; autoresize = RM+BM; tag = 5; layer = <CALayer: 0x7fb508dc5e90>>
| <UIImageView: 0x7fb508dc6540; frame = (0 42; 400 2); autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x7fb508dc6680>>
View not found in container hierarchy: <UITableViewCell: 0x7fb508dbb0a0; frame = (0 0; 320 44); layer = <CALayer: 0x7fb508da98e0>>
That view's superview: NO SUPERVIEW
2015-06-13 12:51:57.498 DigitalBoard[5314:70921] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint:<NSLayoutConstraint:0x7fb508dc0ae0 UITableViewCell:0x7fb508dbb0a0'cellIdentifier'.top == UIView:0x7fb508dc16f0.top> view:<UIView: 0x7fb508dc16f0; frame = (0 0; 400 44); autoresize = RM+BM; tag = 1; layer = <CALayer: 0x7fb508dc0720>>'
*** First throw call stack:
(
etc....
任何人都可以帮助了解究竟是什么问题。
【问题讨论】:
这个问题可能重复:***.com/questions/18177262/… @gyer 不,它不是重复的。因为我已经尝试过这些解决方案但没有工作......这个约束被添加到cellForRowAtIndexPath
【参考方案1】:
您不应将约束添加到vBackView
。我假设vBackView
是cell.contentView
的子视图,在这种情况下,您应该将约束添加到cell.contentView
。
崩溃日志告诉你原因:
当添加到视图时,约束的项必须是该视图(或视图本身)的后代。
将每一行代码更改为开始:[cell.contentView addConstraint:...
。
【讨论】:
请在您将vBackView
添加为子视图的位置显示您的代码,以便我们了解您的视图层次结构。以上是关于添加约束使应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章
来自 ConstraintSet 的 clone 和 applyTo 使应用程序崩溃