17
votes

Could someone help me understand the difference between "trailing space to container" and "bottom space to bottom layout" in interface builder auto layout?

Interface Builder Auto Layout Constraints

I'd like to put a layout constraint in place that ties the bottom of a view to the bottom of it's superview. Both of the above sound like they should accomplish this.

Any clarification would be greatly appreciated.

4

4 Answers

22
votes

Leading/trailing is horizontal spacing. (It's not labeled "left" and "right" because it swaps for right-to-left languages.) Top/bottom is exactly what it says.

"To Container" and "to Layout Guide" are defined differently, but work similarly in practice. "To Container" constraints control the space between a UI element and the edge of its superview (or other containing UI element). "To Layout Guide" constraints are a special case for vertical spacing -- since views extend underneath transparent navigation/status/tab bars in iOS 7, you generally want to control where a UI element appears relative to those instead of to the edge of the view.

So...

I'd like to put a layout constraint in place that ties the bottom of a view to the bottom of it's superview.

"Bottom Space to Bottom Layout Guide" is what you want for that. That'll set spacing relative to the bottom of the superview if there's no tab bar, and relative to the tab bar if one exists.

6
votes

Don't forget to uncheck "Relative to margin" enter image description here

6
votes

Trailing Space

Space from the right edge of the current view to the next/neighbor view or the superview

Leading Space

Space from the a neighboring view or superview to the left edge of the current view

Similarly Top and Bottom correspond to top and bottom space.

0
votes

To add to the already accepted answer, It is also worth noting that if you are replacing the view in a view controller with another, I have found that any constraints to the layout guides are not actually used. I haven't looked into the exact details of this, but I presume it's because the layout guides are only available during the time the View Controller is loaded from the storyboard.

For this reason, I recommend using layout to container, but only when you intend to be swapping views of the view controller.

I came across this when I wanted to use the storyboard to create empty states for my apps.

Example:

-(void)viewDidLoad{ UIView* replacementView = //view loaded from storyboard self.view = replacementView //Layout guide margins will not be followed in replacement view }