Reputation: 12408
I tried a lot in my simple UITableViewCell-Layout to solve autolayout constraint errors, but now got stuck on it. The TableView is rendered well as expected, but the warnings are annoying and i don't like it to run a layout with errors even if it is looking nice.
I removed the labels step by step to see if the constraint errors would go away then. Having only the image at the left and the most top label right to the image is playing nice without constraint errors. But at least i add the second label and give it the same position like the "just now"-label, the errors occur again.
Below i will list the constraints and then the error message from the console:
The constraint errors list the following elements:
(
"<NSLayoutConstraint:0x7fe004aa9010 V:|-(15)-[UILabel:0x7fe004aa83d0'Sunday, March 29, 2015 at...'] (Names: '|':UITableViewCellContentView:0x7fe004aa7bc0 )>",
"<NSLayoutConstraint:0x7fe004aa9150 V:[UILabel:0x7fe004aa83d0'Sunday, March 29, 2015 at...']-(5)-[UILabel:0x7fe004aa8090'Name']>",
"<NSLayoutConstraint:0x7fe004aa9240 V:[UILabel:0x7fe004aa8090'Name']-(5)-[UILabel:0x7fe004aa8920'lorem ipsum blaa foo bar']>",
"<NSLayoutConstraint:0x7fe004aa92e0 V:[UILabel:0x7fe004aa8920'lorem ipsum blaa foo bar']-(33)-| (Names: '|':UITableViewCellContentView:0x7fe004aa7bc0 )>",
"<NSLayoutConstraint:0x7fe004ab09c0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fe004aa7bc0(0)]>"
)
In my opinion there is no constraint doubled and each one has its own meaning. Can someone clarify it to me?
Upvotes: 2
Views: 1349
Reputation: 12408
Finally got it. The Constraints were right. But in my UITableViewCell-Subclass i overwrote layoutSubViews in that way:
- (void)layoutSubviews
{
[super layoutSubviews];
[self.contentView setNeedsLayout];
[self.contentView layoutIfNeeded];
self.textData.preferredMaxLayoutWidth = CGRectGetWidth(self.textData.frame);
}
To remove that code fixed the constraint-warnings. Also, a flicker-effect which occured when display the Table for the first time, also got away :)
Thanks for all the hints and your help!
Upvotes: 1
Reputation: 3494
From the error output, it seems that at some point (maybe before the correct height is calculated) the height of your cell is 0
"<NSLayoutConstraint:0x7fe004ab09c0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fe004aa7bc0(0)]>"
Since you have some positive vertical offsets between your layout elements(like the 15 top offset for the label and so on), the layout cannot be satisfied, because it would mean some views will have to have negative heights, which is not possible.
Since your layout looks good, my suggestion would be to set the priority for the last constraint (the 33 bottom offset from the last label to the container view) to a lower priority (999). This should fix your warnings.
To actually find why your cell has at some point the height of 0, you will have to look to how you calculate the height of the cell (automatic sizing maybe). If you can post the code you are using there, I can try and help you further.
Let me know how it goes. Good luck!
Upvotes: 2
Reputation: 22374
Your constraints declaration
V
for vertical space
and H
For Horizontal Space
label1
is 15
label1
to label2
is 5
label2
to label3
is 5
I just post an image ...and i think the problem is with your first label i.e. "just now" ...just try and check if any warning comes...
if you still encounters a problem then just remove bottom constraint and add height constraint to label3.
Upvotes: 1