1

I explain two different ways to create constraints, that work in similiar way. Is there any difference between those two?

1 method: First method is cntrl+drag from UI object in storyboard. I create couple of screenshot for better understanding:

enter image description here

Then, if we look at created constraint property we will see following: enter image description here

It says - superview leading margin. There is a line from UI object (it is a label) to left edge of screen, but its line not "touching" left edge of the screen, its truncated: enter image description here

2 method: Second method is simply using XCode toolbar Editor->Pin->Leading Space to Superview: enter image description here

Then we come up with following: enter image description here

Line look like this: enter image description here

Obvious, my question could be applied to Trailing Space to Container Margin, top and buttom, because actions are similiar.

What is the difference between those two constraints, create by methods, described above? Is there are actualy difference, or just different names?

Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107

2 Answers2

4

The only difference is that when you use the first approach, it defaults to setting the constraint relative to margin. Margins were introduced in iOS 8 and provide margins that conform to Apple's Human Interface Guidelines. If you're targeting iOS 7 or earlier, don't use the margins. When you use the first approach, hold down Option when the constraint popup appears, and then the choices will drop the Margin bit and will work exactly like the constraints created by your second approach.

If you've already created constraints that are relative to margin and you want them not to be, or vice versa, you can edit the constraint's properties by selecting a constraint in Interface Builder and editing its properties.

NRitH
  • 13,441
  • 4
  • 41
  • 44
1

There are actually a couple other methods of adding constraints in Interface Builder and in the code. They all eventually result in the same constraints and you should simply use the method that works best for you.

Rony Rozen
  • 3,957
  • 4
  • 24
  • 46