4

Imagine a simple UIView like the blue full width container below. It contains a red box and a yellow box.

Simple full width sample

The red and yellow boxes are inset from the container by 10px (I know it doesn't look like it in the image. Forgive my rubbish artwork). I can achieve this by setting a Left constraint on the Yellow box in relation to the UIView (Blue) with a constant of 10.0.

However is this correct? Should I be using the LayoutMargins property or the XXXXMargin layout attributes? Many of the example I have seen simple offset from the left or right

leppie
  • 115,091
  • 17
  • 196
  • 297
Pat Long - Munkii Yebee
  • 3,592
  • 2
  • 34
  • 68
  • do you ever use the Visual Format Language where you could specify that like @"H:|-10-[yellow(==20)]-(>=1)-[red(==20)]-10-|" .. if you want to see how to do that programmatically with VFL then i can paste that in for you. – myte Jan 14 '15 at 10:52
  • actually .. i just read your specification of padding as opposed to offsets per se :) – myte Jan 14 '15 at 11:09

1 Answers1

5

Layout relative to margins is an iOS 8-only feature, and the examples that you see where constraints are set as simple offsets are probably pre-iOS 8. If you're targeting iOS 8 only, then definitely use the margins. To do so, set the layoutMargins property of the blue container to be UIEdgeInsetsMake(0, 10, 0, 10) to pad the left and right edges by 10, or set the NSLayoutAttributeLeftMargin and NSLayoutAttributeRightMargin attributes, and then constrain your yellow and red boxes to those margins. See this writeup for more details.

NRitH
  • 13,441
  • 4
  • 41
  • 44