16
votes

I have some issues with auto layout, hopefully some of you can help me =).

I add a UIView to an empty view controller, like this: enter image description here

Then I add constraints for leading and trailing space, like this:

enter image description here

Which results in this:

enter image description here

I can't understand why it sets them as equal to -16.0. If I change those values to 0, then Xcode complains about the width of the gray UIView.

When I run the app on iPhone 5 simulator, it looks fine: enter image description here

However, when I run the app on iPhone 6 Plus simulator it adds white space on the left and on the right:

enter image description here

I don't want the gray UIView to have any space from the left of from the right no matter how big the screen is. How can I fix it? Thanks!

2
Just click the edit button that's on the right side of the constraints, and change the value from -16 to 0. The -16 has to do with setting the constraints to the margin rather than the edge of the view.rdelmar
If I change those to 0, then Xcode automatically resizes the view like this: i.imgur.com/HjQi2dV.pngalmas
And if I set them to 0, and then manually set the frame from edge to edge, then I get a warning about misplacing views, and at the run time it resizes the view anyway, here is a screenshot: i.imgur.com/954Jz1O.pngalmas

2 Answers

37
votes

Double click the constraint and open it. There select first or second item and deselect "Relative to margin". Then set your constant as 0.

Xcode is showing -16 because it is calculating space with respect to a margin and not the edges.

Check following link for more info on margins. Editing Auto Layout Constraints

enter image description here

0
votes

You can clear all existing constraints on the view,and in Any*Any mode, add constraints for all 4 space to superview, which are Leading, Trailing, Top, Bottom. Then it will have the equal width with the screen no matter how big the screen is.