2

I'm using auto layout to get the heights of rows in UITableView, as described here. That part works.

I stack some labels, so they look like this in Interface Builder:

enter image description here

The constraints are what you'd expect, V:|-lab1-lab2-lab3-|. Problem is, it thinks that's ambiguous and demands you change vertical content hugging priorities.

It shouldn't be ambiguous. The labels should define the height of the row. But IB seems to be taking the height of the row, which you can drag to whatever you want in IB, as something that will constrain the labels, when really at run time the intent is for the label content to determine the height of the row. In this image the row height is slightly larger than it should be at run time, so the green label is getting pulled away from it's text content, and IB starts asking about content hugging priorities. Normally I'd resolve this problem by clicking "Update Frames", which would pull the row height down to the label heights, but "Update Frames" has no effect on row heights in UITableView.

Is there a standard way to resolve this? I would think people hit this all the time if they use auto layout for row heights.

Community
  • 1
  • 1
Rob N
  • 15,024
  • 17
  • 92
  • 165

2 Answers2

0

The issue here is that your constraints doesn't specify any explicit heigh for each labels and it seems that the heigh of your cell is fixed to some value.

First you need to let the table view use UITableViewAutomaticDimension in the method heightForRowAtIndexPath

like this in obj-c :

-(CGFloat) tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return UITableViewAutomaticDimension;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

Then you need to fix height of your labels :

  • Either by setting them equal to each other. That way your cell will be able to calculate it's size.
  • Or you allow them to expend by setting one label content hugging priority to low (this is what you are currently doing)

In your case it seems that you want each label to have the same height, so the first solution should work ;)

RomOne
  • 2,065
  • 17
  • 29
  • I know about `UITableViewAutomaticDimension` and that part is set up. The image is from Interface Builder, not the UI at run time. I shouldn't have to specify explicit heights for labels, because they take their height from their text, and the sum should give the dynamic row height. – Rob N Sep 19 '16 at 02:20
  • I added some more detail and explanation to my question. I'm finding it difficult to explain this problem. – Rob N Sep 19 '16 at 02:41
  • Hey Rob N, actually when I said "fix height" I don't mean explicit height. I meant you should set the height of the first label equal to the second, equal to the third. This will fix their heights at runtime, and you will end up with the layout I think you are expecting (all labels with the same height) – RomOne Sep 19 '16 at 03:22
  • My example looks like it but in the real app, I don't want the labels to be the same height. The labels should have height according to their content - one might have 2 lines, or a bigger font. – Rob N Sep 25 '16 at 22:11
0

I found a solution that worked for me. I'm sorry that the following explanation is hand-wavey, but it's better than I can do myself. It's addressed here (https://useyourloaf.com/blog/table-view-cells-with-varying-row-heights/) under the section Auto Layout of the Prototype Cell.

Auto layout does not know that we will be adjusting the size of the content view to ensure that both labels fit. It therefore expects a hint over which label should expand or compress first to fit the space. To remove the warning lower the vertical compression resistance and increased the vertical hugging priority of the line number label so that it wants to stay at its intrinsic size.

Alan Cham
  • 93
  • 1
  • 1
  • 4