ios - 添加约束使应用程序崩溃

标签 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....

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

最佳答案

您不应将约束添加到 vBackView。我假设 vBackView 是 cell.contentView 的 subview ,在这种情况下,您应该将约束添加到 cell.contentView 中。

崩溃日志告诉您原因:

When added to a view, the constraint's items must be descendants of that view (or the view itself).

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

关于ios - 添加约束使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30816233/

相关文章:

ios - 如何安全地使渲染与更新模型脱钩?

android - 以相同的格式从 iOS 和 Android 获取设备语言

ios - NSUserDefaults 不会一致地加载数据

objective-c - 类型 'SEL' 到 'SEL' 的转换已弃用;改为使用 sel_getName

objective-c - 处理图像时出现 Retina 显示问题

java - 为什么 arraylist 循环在删除元素时崩溃

ios - iPhone 崩溃日志以及如何理解它

为 UIScrollView 获取数据时 iOS 应用程序在 iPad 上崩溃

objective-c - 智能识别电话号码和文本(包括国际号码)

ios - skscene中的暂停/播放问题