Search code examples
iosuitableviewautolayoutconstraints

Autolayout alignment issue


I'm using Autolayout to achieve alignment of two labels in a table cell.

Wanted behavior

Basically, what I need is for the bottom label to align to the left margin of the top label if there is room for all the text, or to ignore the margin if there isn't.

What I did, using the Interface Builder:

  • I pinned the top label to top, trailing and, for starters, gave it a fixed width of 100 (if I don't add this one then the bottom label expands correctly but it also stretches the upper label to stay aligned on the left)

  • Also pinned the bottom label to up, bottom and trailing (priority 1000)

  • Added a leading constraint for the bottom label with a priority of 750

This doesn't seem to work though, the bottom label is always clipped to fit the length of the upper label and I can't figure it out why.


Solution

  • You need to set leading constraint relation of bottom label to "Less than or Equal" to "0" relative to top label.