一切都很顺利,直到升级到 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];
}