I'm having problems with a dynamically-sized tableViewCell in iOS 8. While it visually looks fine, I get log output with AutoLayout errors. I've reduced it down to this simple example.
I'm adding a UILabel to my cell:
self.titleLabel = [[UILabel alloc] init];
self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.titleLabel.numberOfLines = 0;
self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
[self.contentView addSubview:self.titleLabel];
I'm creating my auto layout constraints in code, using Masonry, in 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);
}];
(I could do this in one step with make.edges, but the problem is the same)
This initially looks and works fine. Then, when I perform any modifications to the tableview and call [tableView endUpdates]
(presumably triggering updateContraints
), I get the following in the console log:
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>
I don't understand what the problem is here - I want the label to have padding, but why does that conflict with the overall width of the contentView?
Edit 1:
If I remove the padding, I no longer get the error. Is there some other way I can set it?