Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
314 views
Welcome To Ask or Share your Answers For Others

1 Answer

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":

Constrain to margins checkbox under 'Pin' menu.

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:

Not including margins in the constraints.

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:

Remove the margin from the constraint.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...