具有AutoLayout的UITableViewCell - contentView宽度约束失败

时间:2014-08-23 14:38:12

标签: ios objective-c uitableview autolayout ios8

我在iOS 8中遇到动态大小的tableViewCell问题。虽然它在视觉上看起来很好,但我得到了带有AutoLayout错误的日志输出。我把它简化为这个简单的例子。

我将UILabel添加到我的手机中:

self.titleLabel = [[UILabel alloc] init];
self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.titleLabel.numberOfLines = 0;
self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
[self.contentView addSubview:self.titleLabel];

我在updateConstraints中使用Masonry在代码中创建自动布局约束:

[self.titleLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[self.titleLabel updateConstraints:^(MASConstraintMaker *make) {
    make.leading.equalTo(self.contentView.leading).with.offset(padding.left);
    make.trailing.equalTo(self.contentView.trailing).with.offset(-padding.right);
    make.top.equalTo(self.contentView.top).with.offset(padding.top);
    make.bottom.equalTo(self.contentView.bottom).with.offset(-padding.bottom / 2);
}];

(我可以用make.edges一步完成,但问题是一样的)

这最初看起来很好。然后,当我对tableview执行任何修改并调用[tableView endUpdates](可能触发updateContraints)时,我在控制台日志中得到以下内容:

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) 
(
    "<MASLayoutConstraint:0x7ff4842551e0 UILabel:self.titleLabel.leading == UITableViewCellContentView:self.contentView.leading + 12>",
    "<MASLayoutConstraint:0x7ff484255240 UILabel:self.titleLabel.trailing == UITableViewCellContentView:self.contentView.trailing - 12>",
    "<NSLayoutConstraint:0x7ff484256df0 UITableViewCellContentView:self.contentView.width ==>"
)

Will attempt to recover by breaking constraint 
<MASLayoutConstraint:0x7ff484255240 UILabel:self.titleLabel.trailing == UITableViewCellContentView:self.contentView.trailing - 12>

我不明白这里的问题是什么 - 我希望标签有填充,但为什么会与contentView的整体宽度冲突?

编辑1:

如果删除填充,我不再收到错误。还有其他方法我可以设置吗?

3 个答案:

答案 0 :(得分:3)

您有三个具有相同优先级的约束,这使得系统混淆了如何完全满足它们。     “”     “”     “”

由于您的单元格的宽度也根据您的设备宽度而固定,我建议您对子视图的约束使用一点点弱优先级。

[self.titleLabel updateConstraints:^(MASConstraintMaker *make) {
    make.leading.equalTo(self.contentView.leading).with.offset(padding.left).priority(999);
    make.trailing.equalTo(self.contentView.trailing).with.offset(-padding.right).priority(999);
    make.top.equalTo(self.contentView.top).with.offset(padding.top);
    make.bottom.equalTo(self.contentView.bottom).with.offset(-padding.bottom / 2);
}];

答案 1 :(得分:1)

根据您的问题,我似乎已将标签放在xib或storyboard中的自定义单元格中,现在当您尝试手动设置约束时,它与您的约束不匹配。以编程方式设置。所以我建议以编程方式创建标签并应用约束。

您还可以访问:Creating layout constraints programmatically

编辑1:
尝试浏览此链接可能对您有所帮助,它可能会解决您所面临的问题 http://captechconsulting.com/blog/tyler-tillage/ios-8-tutorial-series-auto-sizing-table-cells

答案 2 :(得分:1)

在使用动态单元格高度计算之前,您是否配置了UITableView?我有类似的问题,这段代码帮助我

- (void)viewDidLoad {
  [super viewDidLoad];
  self.tableView.estimatedRowHeight = 150;
  self.tableView.rowHeight = UITableViewAutomaticDimension;
}

我没有砌体框架的经验,但标准约束对我来说很好。也许你对Masonry格式有问题?

    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    [self.contentView addSubview:self.titleLabel];
    NSArray *constraintsHorizontal = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-12-[_titleLabel]-12-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_titleLabel)];
    NSArray *constraintsVertical = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-12-[_titleLabel]-12-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_titleLabel)];
    [self.contentView addConstraints:constraintsHorizontal];
    [self.contentView addConstraints:constraintsVertical];

https://dl.dropboxusercontent.com/u/48223929/TestTableCellsAutolayout.zip

检查测试项目