gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 10:27:07 31 4
gpt4 key购买 nike

我在 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:

如果删除填充,我将不再收到错误。我可以通过其他方式设置它吗?

最佳答案

你有三个优先级相同的约束,这使得系统混淆了如何同时满足它们。 "", "", ""

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

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

关于ios - 带有 AutoLayout 的 UITableViewCell - contentView 宽度约束失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25463057/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com