Masonry icon indicating copy to clipboard operation
Masonry copied to clipboard

UITableViewCell contentView width issues when laying out subviews

Open slhodson969 opened this issue 11 years ago • 8 comments

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.

slhodson969 avatar Dec 12 '14 12:12 slhodson969

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.

slhodson969 avatar Dec 12 '14 13:12 slhodson969

Hi,slhodson969 : I have the same issue with you,how to resolve?

itdongbaojun avatar Dec 15 '14 02:12 itdongbaojun

@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.

slhodson969 avatar Dec 15 '14 10:12 slhodson969

Hi =) widthissue

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

achoura avatar Dec 24 '14 16:12 achoura

Same issue here. Try @slhodson969 's but no luck.

wongzigii avatar Apr 08 '16 03:04 wongzigii

i end up make the priority small than 1000 and solve the problem

rf695 avatar Apr 26 '16 03:04 rf695

try rewrite -sizeThatFits: for Cell

IUyuRtBbyQnRCI avatar Feb 22 '19 11:02 IUyuRtBbyQnRCI

I end up make the priority between [751,999]. solve the problem

wuzhengbo7 avatar Jan 16 '23 08:01 wuzhengbo7