我在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-|" ];
然后可能将其应用于内容视图? - Mike Welsh