I have a custom UIView for designing UITableViewCell, every things work perfect only it's not get resize properly. And when I am trying to add the constraint it crashed. I used the below code.
- (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;
}
And in log it's shows
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....
Can anyone help to understand what exactly the problem is.