【发布时间】: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