UITableViewCell contentView width issues when laying out subviews
Using Masonry to layout views in a UITableViewCell doesn't seem to work properly. It's as if the contentView isn't having a width set or something.
My code:
self.addressLineOneLabel = [UILabel new];
self.addressLineOneLabel.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:self.addressLineOneLabel];
[self.addressLineOneLabel makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentView).with.offset(10);
make.left.equalTo(self.contentView).with.offset(10);
make.right.equalTo(self.contentView).with.offset(-10);
make.bottom.equalTo(self.contentView.centerY).with.offset(-5);
}];
self.addressLineTwoLabel = [UILabel new];
self.addressLineTwoLabel.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:self.addressLineTwoLabel];
[self.addressLineTwoLabel makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.addressLineOneLabel.bottom).with.offset(10);
make.left.equalTo(self.contentView).with.offset(10);
make.right.equalTo(self.contentView).with.offset(-10);
make.bottom.equalTo(self.contentView).with.offset(-10);
}];
At first, this resulted in a constraint error being shown. However, I set the background color for one of the labels and then the error disappeared and after removing the background color, it's stayed away so I don't have my exact error message. Here's an error from someone else experiencing a similar issue though:
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 ==>"
)
Setting the background color of the label showed me that my constraints aren't being set properly as the colour was only as big as the text in the label. Also, setting the text alignment to center also confirmed this as the text is not centered in the contentView at all.
So I decided to add these lines of code:
self.contentView.backgroundColor = [UIColor redColor];
[self.contentView makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
Which resulted in the text actually being centered and the background colour taking up the full width but with this error:
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:0x7fcd197579b0 UITableViewCellContentView:0x7fcd19755ce0.left == QBAgreementTableViewCell:0x7fcd19754ec0.left>",
"<MASLayoutConstraint:0x7fcd197585a0 UITableViewCellContentView:0x7fcd19755ce0.right == QBAgreementTableViewCell:0x7fcd19754ec0.right>",
"<MASLayoutConstraint:0x7fcd197595f0 UILabel:0x7fcd19758b30.left == UITableViewCellContentView:0x7fcd19755ce0.left + 10>",
"<MASLayoutConstraint:0x7fcd19759720 UILabel:0x7fcd19758b30.right == UITableViewCellContentView:0x7fcd19755ce0.right - 10>",
"<NSLayoutConstraint:0x7fcd194345f0 QBAgreementTableViewCell:0x7fcd19754ec0.width ==>"
)
Will attempt to recover by breaking constraint
<MASLayoutConstraint:0x7fcd19759720 UILabel:0x7fcd19758b30.right == UITableViewCellContentView:0x7fcd19755ce0.right - 10>
So I removed the lines and the original error returned:
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:0x7fb4d1fb7240 UILabel:0x7fb4d1fb5ff0.left == UITableViewCellContentView:0x7fb4d1fb4e40.left + 10>",
"<MASLayoutConstraint:0x7fb4d1fb74f0 UILabel:0x7fb4d1fb5ff0.right == UITableViewCellContentView:0x7fb4d1fb4e40.right - 10>",
"<NSLayoutConstraint:0x7fb4d1e327b0 UITableViewCellContentView:0x7fb4d1fb4e40.width ==>"
)
Will attempt to recover by breaking constraint
<MASLayoutConstraint:0x7fb4d1fb74f0 UILabel:0x7fb4d1fb5ff0.right == UITableViewCellContentView:0x7fb4d1fb4e40.right - 10>
So this is the exact code as I had before but I made a few changes, undid them and this caused the error to return. This seems a bit temperamental :-/
Oh, and my labels are now centered correctly...just have the error.
Hi,slhodson969 : I have the same issue with you,how to resolve?
@itdongbaojun - I haven't really come up with a solution. For now, I have changed my constraints so that they set a width rather then setting a left and a right constraint e.g.
make.width.equalTo(self.contentView);
That seems to haver given me code that works and no error messages.
One thing I noticed is that as you change the code, you get different results so i'm not sure how long this will be a working "solution" for. Hopefully someone here will be able to properly resolve this issue soon.
Hi =)

I have this custom cell that contain a UICollectionView for horizontal scrolling of items . the problem is that the view get sucked at 320pt :( . should I make constraint for leading and trailing ? , knowing that the tableView outlet is connected to the edges in the storyboard.
thanks for help
Same issue here. Try @slhodson969 's but no luck.
i end up make the priority small than 1000 and solve the problem
try rewrite -sizeThatFits: for Cell
I end up make the priority between [751,999]. solve the problem