2

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:

enter image description hereenter image description hereenter image description hereenter image description here

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?

itinance
  • 11,711
  • 7
  • 58
  • 98

3 Answers3

2

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!

Catalina T.
  • 3,456
  • 19
  • 29
  • I changed the Priority of the 33-bottom-constraint to a lower value (900) as you suggested. This changes the warnings from UIView-Encapsulated-Layout-Height to UIView-Encapsulated-Layout-Width: "" – itinance May 02 '15 at 12:03
  • There is no special code for calculating height and constraints. I only use the constraints and in die TableViewController the following lines: self.tableView.estimatedRowHeight = 350.; self.tableView.rowHeight = UITableViewAutomaticDimension; – itinance May 02 '15 at 12:04
  • Hmm.. It is really strange why the width of the cell would be 0.. This should always be the size of the tableView(for which I suppose you have set the constraints) Could you post the entire error you are getting now? – Catalina T. May 02 '15 at 12:06
1

Your constraints declaration

V for vertical space and H For Horizontal Space

  1. vertical space to top from label1 is 15
  2. vertical space from label1 to label2 is 5
  3. vertical space from label2 to label3 is 5
  4. vertical space from 'label3' to bottom is 33
  5. for this refer Auto-layout: What creates constraints named UIView-Encapsulated-Layout-Width & Height?

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

enter image description here

if you still encounters a problem then just remove bottom constraint and add height constraint to label3.

Community
  • 1
  • 1
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
  • Thank you very much for this kind answer, but it doesn't fix this issue. I also added a height constraint to label 3 but the constraint errors remain. – itinance May 02 '15 at 10:06
  • exactly. the warning still comes. – itinance May 02 '15 at 10:11
  • when adding the height constraint, i also removed the bottom constraint. – itinance May 02 '15 at 10:14
  • 1
    then i think the problem is with 5th error....just check the link and check that answer and also the answer below that....its really help it you out – Bhavin Bhadani May 02 '15 at 10:18
  • I completely rebuild it from scratch as you did in your "video" and it worked: https://github.com/itinance/UITableViewAutoHeight So now i have to investigate why it is not working in my already existing project. – itinance May 02 '15 at 11:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76757/discussion-between-bhavin-and-itinance). – Bhavin Bhadani May 02 '15 at 11:12
1

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!

itinance
  • 11,711
  • 7
  • 58
  • 98