Reputation: 13983
I am working on making a table view cell that has the following constraint relationships.
-----------------
| |
| ____________ |
| | | |
| |__________| |
| |
| ____________ |
| |label. | |
| |__________| |
| |
| ____________ |
| | | |
| |__________| |
| |
| ____________ |
| |label. | |
| |__________| |
|________________|
The subviews are as follows: (view, label, view, label). I want the cell to dynamically resize with the two labels, for example, if the first label becomes taller, then the cell should become taller accordingly. If both labels become taller or shorter, the cell should resize accordingly. The two views should have a constant width and height.
If I start with a label I'm fine, a label and a view I'm fine, a label and a label, I'm fine, but if I have 2 labels and a view, as soon as I put the last constraint on the final label, auto-layout throws the error in storyboard (I don't have visible issues when running the app).
The problem is that I don't want to change the compression resistance because I don't want either of the labels to scrunch and I don't see why they would given the provided constraints.
Upvotes: 4
Views: 2488
Reputation: 5679
It seems like you have set the constraints properly, error on storyboards shows for even a tiny change in constraints like 0.5 pixels too. You can remove bottom constraints of last UILabel
and reassign so that conflicting constraints can be rearranged. It will fix this problem.
To get the dynamic cell size:
Set the number of lines for both UILabel
to 0.
Update relation of height constraints of both UILabel's from =
to >=
Apply the following table delegate method in your code
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Use UIStackView
to resolve the problem:
Rather than adding constraints to all the views, you can take all the view's in a stack view and just assign the top, bottom, leading and trailing constraint to stack view and keep table delegate method. I think this will be more appropriate way.
Upvotes: 1
Reputation: 64
Modify last label's bottom constraint to a constant with relation "Greater Than or Equal"
Your code should look like
Hope this works
Upvotes: 4