derdida derdida - 5 months ago 25
iOS Question

Minimum cell height - Autolayout

I have some Autolayout Cells - but would like that they have a minimum height, of the second Label does not contain any value (for example in an form)

I thought the easiest way should be to create an additional Constraint with Priority 999 to setup an minium height, like in that pic:

enter image description here

But then, the label ("Mayer Thomas") is not self-sizing anymore.

Whats the best way to solve such things? I could create 2 layouts, but in my opinion that should be not a good solution.

Answer

Set the height constraint of the optional label as greater than or equal to whatever height you want. Also, set the compression resistance and content hugging of the label to required. Your label should now consume extra height if there is more content, or just take the minimum size that was set.

[label addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:15/*the min height you need*/]];
[label setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];