【发布时间】: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....
任何人都可以帮助了解究竟是什么问题。
【问题讨论】:
-
@gyer 不,它不是重复的。因为我已经尝试过这些解决方案但没有工作......这个约束被添加到
cellForRowAtIndexPath
标签: ios objective-c crash autolayout