1

I am using custom cell in UITableView. There are 4 views in cell. 3 UILabels and 1 UIImageView (grey colored one) as shown below. Text of labels are dynamic so width and height is dynamic.

enter image description here

Here width of UIImageView depends on 2 labels. So issue is to specify constraints between labels and UIImageView that will decide the width of UIImageView.

See below what happens if first label's text is long.

enter image description here

How to specify constraints when width depends on width of multiple labels?

Update : I tried setting number of lines for labels to 0 and intrinsic size to placeholder. It's not working, too. Below is how it looks. I tried setting intrinsic width and height to none, but it gives error when I do that for both label, not giving error if set that for only one label.

enter image description here

enter image description here

enter image description here

Update : I changed properties and below is displayed what are they now and how the cell is displayed.

Cell :

enter image description here

Constraints for label in first row (pink colored) :

enter image description here

Constraints for label in second row (cyan colored) :

enter image description here

Geek
  • 8,280
  • 17
  • 73
  • 137
  • Do you set your constraints in IB or in code? – Martin Koles Mar 04 '14 at 13:29
  • If in IB, you need to set number of lines for the label to 0 and in Size Inspector the Intrinsic Size to Placeholder. This way the size of the label and number of lines will be calculated at runtime based on content. – Martin Koles Mar 04 '14 at 13:33
  • And obviously follow this post http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights. But I saw you there already. – Martin Koles Mar 04 '14 at 13:35
  • @MartinKoles I'll try changing number of lines and intrinsic size. Can you please explain why to set intrinsic size to placeholder? – Geek Mar 04 '14 at 15:48
  • See this http://stackoverflow.com/questions/19677142/what-is-the-difference-of-intrinsic-size-vs-system-width-height-constraints – Martin Koles Mar 04 '14 at 16:15
  • @MartinKoles Settings lines to 0 and intrinsic size to placeholder is also not working. See my updated question. – Geek Mar 05 '14 at 04:58

1 Answers1

2

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