添加约束会导致应用程序崩溃

时间:2015-06-13 07:33:21

标签: ios objective-c crash autolayout

我有一个用于设计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....

任何人都可以帮助理解究竟是什么问题。

1 个答案:

答案 0 :(得分:0)

您不应将约束添加到vBackView。我假设vBackViewcell.contentView的子视图,在这种情况下,您应该将约束添加到cell.contentView

崩溃日志告诉您原因:

  

添加到视图时,约束的项必须是该视图的后代(或视图本身)。

将每行代码更改为:[cell.contentView addConstraint:...