在64位iOS7.1模拟器上运行时,Autolayout代码崩溃

时间:2014-03-18 09:04:46

标签: ios autolayout tableviewcell arm64

一切顺利,直到升级到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.

导致崩溃的代码是使用autolayout的自定义表格视图单元格,单元格具有动态高度,可以调整内容高度,这是我的约束设置代码:

-(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];
}

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:1)

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