2
votes

I'm trying to use auto layout to dynamically size my UITableViewCell heights. For basic cell design, like to labels in row, I've managed to achieve what I want. But I can't get it working for the following:

Setup

  • I'm using Swift and Xcode 6.4 targeting iOS8 and above.
  • TableView row height is set to UITableViewAutomaticDimension.
  • Estimated height for TableView is set to 100, which should generally be close to the final height.
  • The title's vertical hugging priority is set to low (250).
  • Didn't change the other elements hugging and compression priority.

Here's the prototype cell:

Prototype cell

The image to the left has a fixed height. The title text should grow to fit the complete text. It's a label with rows set to 0. The subtitle should not grow because it will only be short text.

My constraints setup

  • For the image leading and top constraint set to 18. Trailing constraint to title and subtitle label set to 18. Bottom constraint set to 18 with priority low (249).
  • For the title label top and trailing constraint to superview set to 18. Leading constraint to image 18. Bottom constraint to subtitle is 0.
  • For the subtitle label top constraint to title label is 0. Trailing space to superview is 18. Leading constraint to image is 18. Bottom constraint to superview is 18 with priority low (250).

The Problem

No matter what, I want the bottom space to the superview to be 18 units. That means, if the text's (title + subtitle) height is less than the image's height. The image's bottom constraint should ensure 18 unit bottom margin. If the title grows in height and the total text is higher than the image, the subtitle's bottom constraint should ensure 18 unity bottom margin and the image's bottom constraint should be ignored.

I'm thankful for any suggestions how to solve this, because I'm at a point, where I'm just punching in some random values to the constraints...

Update

Please see the marked answer on how to setup constraints to achieve the layout I needed. If your cells do not layout properly on the tableview's first load, please se this question for a workaround: iOS 8 Auto height cell not correct height at first load

1
post the images as links, a mod (e.g. me) will include the images afterwards. Or post them by your own (after my upvote is considered)luk2302
This is not a swift question, please don't use the swift tag just because there is some swift code in your project. "Use this tag only for questions that are specific to Swift language features, or those that require code in the language. Use the related tags [ios], [osx], [cocoa-touch], and [cocoa] for (language-agnostic) questions about the platforms or frameworks.[..]"Stephen Groom
@StephenGroom thank you, I will keep that in mind for future questions.Dino Fejzagic
Can you describe any width or horizontal constraints you have? Are there any red errors in IB?Stephen Groom
Here are the horizontal constraints from left to right. 1. ImageView leading space to superview 18. 2. ImageView trailing space to titleLabel 18 and trailing space to subtitle label 18. 3. title and subtitle label trailing space to superview 18. No red errors in IB.Dino Fejzagic

1 Answers

2
votes

Firstly, UITableViewAutomaticDimension despite the name isn't documented for auto TableView row heights. Just don't implement heightForRowAtIndexPath and implement estimatedheightForRowAtIndexPath as you have.

To address the layout problem, it is not a priority issue but a problem with your constraints. Because you have a fixed top and bottom constraint from your image, the cell will never grow to be larger than the image + 36pt. The solution is to make your top and bottom constraints from your image >= rather than =.

After doing this, you will probably find that there are red warnings in IB for ambiguous constraints. This is because with greater than or equal to constraints, the layout can't determine the correct Y position of the image view. The solution: add a center Y in superview constraint to the imageView.