Martin
Martin

Reputation: 12203

Autolayout: why are these same constrains different?

I'm learning autolayout. I'm currently adding a simple constraint: 0px margin between a view and it superview.

Apparently, there are two ways to add this constraint:

First, click on the lower right button of your xib editor, select the left margin and click add n constraints, as in the given screenshot: first way to add constraint

Here's the first constrain properties. It has a 16px constant margin:
enter image description here

Second way, select in Xcode menu: Editor > Pin > Leading space to superview:
The second way

Now, my constraint has 0px, but it works too: the view is well adapted according to its superview

Here's my second constraint properties:
enter image description here

First question: why my first constraint left and right margin are 16px? I'm working on a brand new project! If I set 0px, there is some extra space around my view!

Second question: what is the difference between these two constraints? What is the best method?

Thanks

Upvotes: 0

Views: 347

Answers (1)

matt
matt

Reputation: 534885

You've already given the answer. A constraint to the superview is not the same as a constraint to the superview's margin.

It might be easiest if you think about how you'd form these constraints in code. Here are the attributes you can use:

https://developer.apple.com/Library/ios/documentation/AppKit/Reference/NSLayoutConstraint_Class/index.html#//apple_ref/c/tdef/NSLayoutAttribute

As you can see, there is both Right and RightMargin. Pinning a subview's right edge to its superview's Right is not the same as pinning it to its superview's RightMargin.

Upvotes: 1

Related Questions