254
votes

I am Working with autolayout and constraints and found there is a Constrain to margins option in Xcode 6 which was not present in Xcode 5 and is checked by default.

I created a test project then I added a UITableView on a ViewController with the frame set to the same size as view and added constraints

Xcode 6 You can see here even though tableview has the same frame as view Xcode suggests to add -16 as constraint whereas Xcode 5 would suggest adding spacing 0.

With Constrain to margin checked

Now when you uncheck "Constrain to margin" option it behaves same as Xcode 5 and would suggest adding 0 as constraint

With Constrain to margin UnChecked

Also, I found that once I add constraint with Constrain to margin checked, I am no longer able to open the storyboard file in Xcode 5 so it's definitely something new in Xcode 6

Hopefully, I am able to explain my question properly. I would like to understand what "Constrain to margin" actually does and when I should and should not use it. I do apologize if it's something very simple and obvious.

EDIT

I found something about layout margins in discussion here , I wonder if it's related to this.

3
No need to apologise - it's not obvious what that does at all.Ben Clayton
+1 no need to apologize, I wanted to ask this question, too. BTW: to open the storyboard in Xcode5 have a look at: stackoverflow.com/a/25298909/529243Olaf
This is the closest thing to an explanation I have found : stackoverflow.com/questions/25275901/…Nathaniel
How to disable this?orkenstein
Yeah thats the real pain . I haven't been able to figure out , how to uncheck it permanently.Bhumit Mehta

3 Answers

325
votes

I don't understand at all why people are complaining that "Margins would cause an outright crash on anything prior to iOS 8."

Setting your constraints relative to margin in a xib file or storyboard DOES NOT make your app crash on iOS7, and it DOES NOT make a UI difference on your iOS7 device neither, as long as you don't touch the UIView.layoutMargins and UIView.preservesSuperviewLayoutMargins properties in your code.

What is Margins in iOS8

Layout margins represent padding around the interior of a UIView that the layout system can use when laying out subviews - to ensure that a gap is left between the edge of a view and a subview. In this respect it is very much like the padding property associated with blocks in CSS.

enter image description here

By default, a UIView has layout margins of 8 points on each side, and this can not be changed in Interface Builder. However, by setting the UIView.layoutMargins property in the code, which is only available on iOS8, you are able to adjust these values.

You can get IB to display the margins with Editor > Canvas > Show Layout Rectangles: enter image description here

Margins can be used to help layout your views and subviews. Every UIView come with margins by default, but they only affect view placement when you set up a constraint that is related to a margin.

How to use Margins

The only way to use margins in Interface Builder is to check the Relative to margin option while configuring your constraints. This is how you direct your constraint to Use margins instead of edges when laying out my view.

enter image description here

Let's take a look at four different ways of setting up a leading constraint between a view and its subview. For each constraint we review the first association described will be the subview's leading, and the second will be superview's leading. What you want to pay close attention to is the check and uncheck status of the Relative to margin option of each constraint end, because that defines whether the constraint is tied to the margin or the edge of the view.

  1. First item(uncheck), second item(check): In this case, we're declaring that subview's left edge should align to superview's left margin(as shown in this image).

enter image description here

  1. First item(uncheck), second item(uncheck): Both using edge, not margin. In this case, we're declaring that subview's left edge should align to superview's left edge.

enter image description here

  1. First item(check), second item(uncheck): In this case, we're declaring that subview's left margin should align to superview's left edge. This kind of layout actually makes the subview overlap the superview.

enter image description here

  1. First item(check), second item(check). This actually has a same effect as case 2, since both subview and superview has a same default margin. We're declaring that subview's left margin should align to superview's left margin.

enter image description here

What is good about Margins

This new feature (iOS8) only impacts UI development if you decide to use margins.

By using margins you can adjust the placement of multiple subviews that share a common relation to a shared superview by changing the value of a single property. This is a clear win over setting all associated constraints with fixed values, because if you need to update all the spacing, instead of changing each value one by one, you can simultaneously modify all relevant placement by updating the superview's margin with a single line of code like this one:

self.rootView.layoutMargins = UIEdgeInsetsMake(0, 50, 0, 0);

To illustrate this benefit, in the following case all subviews' left edges are aligned to their superview's left margin. Thus, changing superview's left margin will affect all subviews at the same time.

enter image description here

63
votes

In iOS 8 you now have the option to define your constrains relative to a predefined margin to the superview's bounds, instead of the superview's bounds themselves. Yes, it is totally related to the layout margins you pointed to in the docs. One advantage is that you may redefine your margins dynamically, or differently for each kind of device, and the layout will be updated correspondingly without modifying the constraints.

When to use it: when you want to take advantage of this new flexibility.

When to NOT use it: for any app targeted to run on iOS 7 or below.

42
votes

The property on UIView is: layoutMargins. See the Apple Docs. Basically if the layout margins are 8,8,8,8 (the default), a constraint with 0 leading space to container margin will have an x position of 8. Note that this is only available on iOS8 or later.

For everyone who doesn't want their constraints to go to the container margin:

CTRL+click+drag to show the constraint creation popup.

If the menu shows to create the constraint to the margin by default, hold down option/alt to allow the constraint to be made to the container and not the container margin.

Now it will show the option to create the constraint NOT to the margin. This is WAY faster in my usage.