1

I'm new to using constraints in my iOS projects, and trouble setting the right constraints for my dynamic UITableViewCell. I've tried every combination I can think of but it either won't dynamically change height, or it gives me warnings about ambiguous layouts.

My first label1 is not supposed to change in height, but the other two are. My current constraints achieve the desired effect, but are giving me the warnings seen under. enter image description here

enter image description here

The warnings go away if I constrain the height of the labels (obviously), but that doesn't solve my problem.

Any suggestions on how to solve this would be appreciated!

1 Answer 1

1

First of all you need to set

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44.0

in viewDidLoad of your UITableViewController

Then you need to set constraints from top to bottom of the cell

top space to superview
vertical spacing
bottom space to superview
height greater or equal

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

and of course set lines to 0 for every UILabel

Example project is available here https://github.com/MihaelIsaev/SwiftAutoResizableCells

Sign up to request clarification or add additional context in comments.

6 Comments

When following your steps, I run into another problem, the first time the cells are used, it does not dynamically change height, but after scrolling the table view and letting the cells be reused, they look perfect. Any suggestions?
Are you tried to run my example project? The same problem in my project? I tested it and it works perfectly. Maybe you missed something?
I could not locate the xib-file for your custom cell in the project, so I could not compare your constraints with mine. Would you please explain where it is, or send me an image of your constraints?
I just use storyboard, you can find my cell prototype in the Main.storyboard. And you can download whole project, open it in Xcode and run.
Solved it by programmatically scrolling the view so that the cells would reuse without the user knowing, thanks for the help with the constraints!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.