What does Constrain to margins mean in Interface Builder in Xcode 6.0.1
iOS8 introduces a new property of the UIView: layoutMargins
. Shortly: these 4 values (a UIEdgeInsets
) represent the margins of the view: its subviews can now be positioned relative to these margins, or the old way relative to the sides.
I wrote an article about this in my blog with more details, let me know if it is clear and if you have additional doubts.
What is Constrain to margin in Storyboard in Xcode 6
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
andUIView.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.
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:
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.
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.
- 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).
- 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.
- 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.
- 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.
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.
why do i get Must translate autoresizing mask into constraints to have _setHostsLayoutEngine:YES in xcode 6 beta
Not sure how much this helps, but I see the same error if I try to place a cell directly in a UIView rather than in a table. This worked fine with Xcode 5 so I suspect it is a bug with XCode6/ios8. In the mean time, debug into that method and look at your tableView. Make sure it actually has an instance of the cell you are trying to retrieve.
Update: Filed a bug report with Apple and I can confirm that this is an ios8 issue. Unfortunately it is still there as of beta-5, hopefully they will have it fixed before the final release.
Related Topics
How to Use Socket in Swift (Connect, Send and Get Message)
How to Give PDF Data a Filename for User to Save in Swift
Create a New Window with Nswindow
Cllocation Distancefromlocation (In Swift)
A Codable Structure Contains a Protocol Property
Protocol Extension Initializer Forcing to Call Self.Init
Using Animoji/Memoji as Profile Photo
Add Datepicker in Uiactionsheet Using Swift
Combined Chart (Line- and Bar Chart) Using iOS-Charts
Loop Over Multiple Uialertcontroller'S
How Does Let X Where X.Hassuffix("Pepper") Work
Cllocation Distancefromlocation (In Swift)
Safari App Extension Crashes After a Few Seconds for Hello World Project
How to Migrate Core Data's Data to App Group's Data