2

一切都很顺利,直到升级到 Xcode 5.1,当我尝试在 64 位 iOS7.1 模拟器上运行它时,我的应用程序崩溃了,但在 32 位模拟器上仍然很好。

崩溃消息在这里:

2014-03-18 16:58:31.451 MyApp[8645:90b] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.   Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x109391530 V:|-(5)-[UILabel:0x109390630]   (Names:     '|':UITableViewCellContentView:0x109390060 )>",
    "<NSLayoutConstraint:0x109391580 V:[UILabel:0x109390630]-(3)-[UIView:0x109390960]>",
    "<NSLayoutConstraint:0x109391600 V:[UIView:0x109390960(40)]>",
    "<NSLayoutConstraint:0x109391650 V:[UIView:0x109390960]-(5)-|   (Names: '|':UITableViewCellContentView:0x109390060 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x109398d10 h=-&- v=-&- UITableViewCellContentView:0x109390060.height == UITableViewCellScrollView:0x10938f8a0.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x1093997d0 h=-&- v=-&- UITableViewCellScrollView:0x10938f8a0.height == TDStatusFeedCell:0x10938f590.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x10939a4d0 h=-&- v=--& V:[TDStatusFeedCell:0x10938f590(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x109391580 V:[UILabel:0x109390630]-(3)-[UIView:0x109390960]>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in  <UIKit/UIView.h> may also be helpful.

导致崩溃的代码是使用自动布局的自定义表格视图单元格,该单元格具有动态高度,它将适应内容高度,这是我的约束设置代码:

-(void)setupConstraints {
    self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    _ivPhoto.translatesAutoresizingMaskIntoConstraints = NO;
    _lblTitle.translatesAutoresizingMaskIntoConstraints = NO;
    _imageContainerView.translatesAutoresizingMaskIntoConstraints = NO;


    NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:_ivPhoto attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:40];
    [self.contentView addConstraint:constraint];

    constraint = [NSLayoutConstraint constraintWithItem:_ivPhoto attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:45];
    [self.contentView addConstraint:constraint];

    constraint = [NSLayoutConstraint constraintWithItem:_ivPhoto attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1 constant:10];
    [self.contentView addConstraint:constraint];

    constraint = [NSLayoutConstraint constraintWithItem:_ivPhoto attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1 constant:10];
    [self.contentView addConstraint:constraint];

    constraint = [NSLayoutConstraint constraintWithItem:_lblTitle attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1 constant:60];
    [self.contentView addConstraint:constraint];

    constraint = [NSLayoutConstraint constraintWithItem:_lblTitle attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTrailing multiplier:1 constant:-10];
    [self.contentView addConstraint:constraint];

    constraint = [NSLayoutConstraint constraintWithItem:_imageContainerView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTrailing multiplier:1 constant:-10];
    [self.contentView addConstraint:constraint];

    constraint = [NSLayoutConstraint constraintWithItem:_imageContainerView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:_lblTitle attribute:NSLayoutAttributeLeading multiplier:1 constant:0];
    [self.contentView addConstraint:constraint];

    NSArray *verticalConstriants = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[_lblTitle]-3-[_imageContainerView(40@1000)]-5-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_lblTitle, _imageContainerView)];
    [self.contentView addConstraints:verticalConstriants];
}

编辑

(@Christoper)单元格高度计算的代码在这里:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
TDNewsFeed *feed = _newsFeeds[indexPath.row];
if (feed.status) {
    _sizingCell.frame = tableView.bounds;

    _sizingCell.lblTitle.text = [feed title];
    [_sizingCell setNeedsLayout];
    [_sizingCell layoutIfNeeded];
    CGSize size = [self.sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    return size.height + 1;
}

return [TDReviewCell cellHeight];
}
4

2 回答 2

6

确保在-tableView:heightForRowAtIndexPath:你返回 CGFloat 而不是浮动。float 可能会导致 64 位上的零高度单元格,这也会破坏您的布局约束。我同意克里斯托弗 - 你应该从单元格高度计算中粘贴你的代码。

于 2014-03-19T06:35:17.457 回答
1

只有当您的 contentView 正好是 53 + _lblTitle 的高度时,您的约束才会起作用。也许计算单元格高度的代码与自动布局的 _lblTitle 高度不同?它是否应用了您在约束中创建的相同的左右边距?

于 2014-03-19T06:15:14.503 回答