Padding or margin for UILabels in UIStackView

hanneskl picture hanneskl · Oct 25, 2015 · Viewed 10k times · Source

I want to add margins or paddings on the right of some nested UILabels

View hierarchy

like shown here:

Target layout

Using trailing spaces on the labels seems wrong and old-school somehow.

Any way to make that more elegant on interface builder?

Answer

rob mayoff picture rob mayoff · Oct 25, 2015

Just add a spacer view at the end of the horizontal stack view. Set its color to clear. Give it a width constraint to fix its size.

Or, judging from your screen shot, it looks like you could just move the right edge of the top-level stack view (child of MasterCell) to leave a margin.

UPDATE

You can set layout margins on a stack view, in the Size inspector:

layout margins

Maybe you just want to set the right layout margin.

If you set the stack view's layout margins in code (the layoutMargins property), you must also set its layoutMarginsRelativeArrangement property to YES.