0

I use autolayout. I am displaying 2 labels in custom UITableViewCell. Label1 is above Label2. Their text is dynamic.

The issue is the height of one of the labels when displayed is larger than its text. I tried changing their Content Hugging Priority.

So what happens is, if that priority is same or Label1's priority is higher, then Label1 is having exact height to fit its text but Label2 has larger hight than required. And when Label2's hugging priority is higher than issue is with Label1's height.

Any idea how to solve it?

Geek
  • 8,280
  • 17
  • 73
  • 137
  • Do the cells have a variable height? – jrturton Mar 03 '14 at 07:42
  • Most of the time when I experience this kind of problems, it is because of an extraneous constraint. Are your constraints added in code or IB? Have you tried removing all constraints and add them again? Does the console give you any layout related messages? – Marius Waldal Mar 03 '14 at 09:54
  • @MariusFalkenbergWaldal I have tried adding constraints from scratch few times. No, console does not give any error. – Geek Mar 03 '14 at 11:37
  • Are you using interface builder, or are you adding constraints in code? – Marius Waldal Mar 03 '14 at 12:02
  • @MariusFalkenbergWaldal I am adding constraints in IB. – Geek Mar 03 '14 at 12:25

2 Answers2

0

It looks like you are expecting your cells to auto-size based on auto-layout's constraint information, but UITableView sizes it's cells using frames/autosizing masks. If this is the expectation and you aren't autosizing the cells like in this question, then your labels are going to either force-clip themselves in order to fit inside the cells with the sizes they were given from the table view or grow to satisfy all of the margin-constraints.

Since the content hugging and content compression resistance priority values are less than required (less than 1000), they are considered "optional" and will be satisfied as close as they can be without violating any of the other required constraints. This is why your label begins growing (or clipping itself).

This can be solved in a couple of ways off the top of my head:

  1. If you don't care about the cell having a variable height and are fine with the labels migrating toward the top of the cell, then make the constraint that pins the bottom label to the lower edge of the superview be non-required. More specifically, make that constraint have a priority lower than the vertical contentHuggingPriority for both of the labels. This way the content hugging priority constraints will take precedence over the lower constraint.
  2. Make your cells auto-size themselves (using auto layout or otherwise) so that the system never has to consider the vertical contentHuggingPriority of each label 'optional'.
Community
  • 1
  • 1
larsacus
  • 7,346
  • 3
  • 26
  • 23
  • If one of your labels is taller than it needs to be, but the other is sized correctly, how can the cell be the correct size? What happens when you try #1 in my answer? Are the labels sized correctly, but just aligned more toward the top of the cell? Or something different? – larsacus Mar 04 '14 at 05:12
-1

I solved this after experimenting with lot of things. The only thing I had to do is to set horizontal and vertical content compression resistance priority to required.i.e. 1000. I did this for all labels because I don't want any of the labels to trim their content.

One more thing which is too much important is Getting Right Height Of Cell. If there is even 1pt of error in calculating custom cell's height it will not be displayed as expected.

Hint :

  1. If height of any view is greater than expected then possibly calculated height of cell is greater than what is actually required.
  2. If any of views is shrinking vertically or not displaying whole content then possibly calculated height of cell is lesser than what is actually required.

Yoy can test if height is wrong by adding/removing constant value to height (variable) you calculate for cell.

Geek
  • 8,280
  • 17
  • 73
  • 137