12

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?

hanneskl
  • 193
  • 1
  • 2
  • 6

1 Answers1

34

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.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • 2
    I have been searching high and low to figure this out. Thanks for the updated answer. Why that Layout Margins is collapsed by default is a mystery, but I didn't see it until reading this post. Thanks. – Don Fitz Mar 24 '16 at 22:25
  • 4
    I tried this in Xcode 8.2, but its getting crash. Anyone knows why ? – Sneha Feb 13 '17 at 06:44
  • I faced collapsing bottom AutoLayout margin when hiding the last (at very bottom) view in the stack, setting fixed margins helped – schmidt9 Nov 02 '17 at 13:40
  • One problem with this approach is that it forces the left - right choice on you, not leading - trailing. – MarkAurelius Sep 24 '18 at 09:25