Search code examples
iosuitableviewswiftautolayoutnslayoutconstraint

Content hugging priority Dynamic Cell Size


Recently I've had a complex auto layout situation that has been solved (thanks to @Catalina T.). Currently I'm experiencing an issue with Content Hugging Priority. I have 4 dynamic labels that may contain huge texts so cells should fit the content. With the current constraints and priorities if I set huge texts to all dynamic labels it works perfectly (though one of them won't be displayed fully instead it will be clipped with dots). If I set the third label's text to a huge one auto layout goes crazy... I get something like this:

enter image description here

Whereas I want all the labels to be arranged consequently.

Demo project.

How to fix this issue?

Thank you in advance!


Solution

  • I managed to solve this issue.

    AutoLayout should know leading, trailing, top and bottom constraints from all your dynamic views. Besides that it might happen that one view is huge and others are not, as a result you'll get the wrong arrangement. To solve this simply add Vertical Spacing for each dynamic views that are not yet connected with this constraint. Then you should change the constraint Relation to Greater Than Or Equal, so you'll simply notify the AutoLayout that you want these views to be arranged consequently.

    Solved demo project.