【问题标题】:UITableViewCell with AutoLayout - contentView width constraint failure带有 AutoLayout 的 UITableViewCell - contentView 宽度约束失败
【发布时间】:2014-08-23 14:38:11
【问题描述】:

我在 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];

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

[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:

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

【问题讨论】:

  • 您是否尝试过使用视觉格式? NSArray *formats = @[ @"H:|-12-[tl]-12-|", @"V:|-6-[tl]-6-|" ]; 然后可能将它们应用到内容视图?
  • 感谢您查看我的问题!不幸的是,使用视觉格式语言会导致与上述完全相同的错误。 ://
  • 您是否尝试过使用 >= 而不是 == ?
  • 使约束greaterThanOrEqualTo 不起作用-标签的右边缘可以随意扩展,因此标签变成单行并且单元格非常宽。尝试lessThanOrEqualTo 会导致与equals 相同的错误。

标签: ios objective-c uitableview autolayout ios8


【解决方案1】:

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

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

[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);
}];

【讨论】:

  • 不敢相信我以前没有尝试过,但重新开始对我来说非常有用。最后,谢谢!
【解决方案2】:

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

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

我没有使用 Masonry 框架的经验,但标准约束对我来说很好用。也许你的 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查看测试项目

【讨论】:

  • 是的,我很确定我已经正确配置了它。我的 viewDidLoad 中有这个确切的代码(虽然我的身高是 100)。
  • 我尝试通过标准约束构造函数来实现它,它对我有用。我认为Masorny框架可能有问题
【解决方案3】:

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

您也可以访问:Creating layout constraints programmatically

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

【讨论】:

  • 迟到的回复 - 单元格不是来自笔尖或情节提要,而是完全在代码中。我改用笔尖,但仍然遇到同样的错误。
  • 您好,抱歉回复晚了。请访问链接,尽管在情节提要中设置了约束,但它可能会有所帮助,但可以通过编码进行类似的设置。 appcoda.com/self-sizing-cells
猜你喜欢
  • 2015-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-23
  • 2018-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多