ios - 在 64 位 iOS7.1 模拟器上运行时自动布局代码崩溃

标签 ios autolayout uitableview 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.

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

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

最佳答案

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

关于ios - 在 64 位 iOS7.1 模拟器上运行时自动布局代码崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22474599/

相关文章:

ios - SDWebimage 在自定义 UITableViewCell 中加载图像,但是当滚动图像时随机在其他单元格中加载

ios - UITableViewCell ImageView 缩放不正确?

ios - 在 IB 中使用 Autolayout 如何以编程方式将 UIView 定位在屏幕中央?

ios - 使用数组项属性值显示在 UITableView/UICollectionView 中

ios - 如何仅在表格 View 的最后一部分添加页脚文本?

iOS自定义键盘加载问题

ios - 如何在深度链接或关联Domain iOS中的应用中打开选定的URL

ios - 无法在我的 ViewController 中显示 tableview

ios - iOS 6 自动布局问题

ios - UITableView 自动调整行大小约束在 iPhone 6Plus 上神秘破坏