UIViewController Nested View To Screen Edge

2019-09-10 19:02发布

问题:

I have a nested view inside my view controller and when i set the leading and trailing (to superview) constraints to 0, the subview spans between left and right edges of the superview leaving a margin on both sides. How do I remove this margin without adding a negative value in the constrains?

回答1:

Assuming you're using Xcode 6...

There are two things to keep in mind when setting Auto Layout constraints regarding margins:

1. Adding new constraints

Xcode defaults new constraints to include margins. You'll find this on the 'Pin' menu - there's a checkbox that defaults to "Constrain to margins":

In the above screenshot, the left and right constraints both include margins. Unchecking this box will show the distance in points not including the margins:

2. Removing the margin calculation from an existing constraint.

You can adjust a constraint to not include the margin by selecting the constraint and accessing the Size Inspector (5th icon in right-hand panel, or Command+Option+5) and editing the portion that is relative to the margin. Access the dropdown and unselect the "Relative To Margin" option:

Note that the constant value itself will stay the same, but the object will adjust and not longer take the margin into consideration.

Lastly, I haven't found a way to turn this behavior off.. I would prefer to not have constraints set against the margins by default, but as far as I am aware with Xcode 6.1.1 there isn't a way to change this behavior.