我在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
:
[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的整体宽度冲突?
如果我删除了填充,我将不再收到错误。还有其他设置方法吗?
您具有三个具有相同优先级的约束,这会使系统完全困惑于如何满足它们。“”,“”,“”
由于单元格的宽度也根据设备的宽度而固定,因此我建议对子视图的约束使用一些较弱的优先级。
[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);
}];
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句