Stack views provide a way to horizontally or vertically position a series of views. By
configuring a few simple properties such as alignment, distribution, and spacing,
you can define how the contained views adjust themselves to the available space.
While the stack view takes care of positioning the buttons, you still need to add Auto Layout constraints to position the stack view itself.
When you embed a view in a stack view, any constraints to other views are removed.
distribution = Fill 的情况下:拉伸hugging priority最低的;如果优先级相同,拉第一个
The property that determines how a stack view lays out its views along its axis is its distribution property.
Currently, it's set to Fill,
which means the contained views will completely fill the stack view along its axis.To accomplish this, the stack view will only expand one of its views to fill that extra space;
specifically, it expands the view with the lowest horizontal content hugging priority, or if all of the priorities are equal, it expands the first view.
distribution = Equal Spacing
layout guides and layout anchors.
NSLayoutAnchor
UILayoutGuide
let constraint = NSLayoutConstraint(item: topLabel, attribute: .Bottom, relatedBy: .Equal, toItem: bottomLabel, attribute: .Top, multiplier: 1, constant: 8);
let constraint = topLabel.bottomAnchor.constraintEqualToAnchor(bottomLabel.topAnchor, constant: 8); //iOS 9 之后
or .LeadingMargin aren't available directly. UIView has a new property called layoutMarginsGuide, which is a UILayoutGuide (you'll learn about these in the next section).
To summarize:
-
Layout anchors are a quick way of making constraints between different attributes of a view
-
There are three different subclasses of NSLayoutAnchor
-
The compiler will prevent you from creating constraints between the different subclasses of NSLayoutAnchor
- The specific subclasses of NSLayoutAnchor are:
-
NSLayoutXAxisAnchor for leading, trailing, left, right or center X anchors
-
NSLayoutYAxisAnchor for top, bottom and center Y anchors
-
NSLayoutDimension for width and height
Layout guides
A layout guide gives you a new way to position views when you'd previously need to use a dummy view.
For example, you might have used spacer views between buttons to space them equally,
or a container view to collectively align two labels. But creating and adding a view has a cost, even if it's never drawn.
Think of a layout guide as defining a rectangular region or a frame in your view hierarchy, the edges of which you can use for alignment.
Layout guides don't enable any new functionality, but they do allow you to address these problems with a lightweight solution.
You add constraints to a UILayoutGuide in the same way that you add them to a UIView,
because a layout guide has almost the same layout anchors as a view — dropping just the inapplicable firstBaselineAnchor and lastBaselineAnchor.
The UILayoutGuide class is designed to perform all the tasks previously performed by dummy views,
but to do it in a safer, more efficient manner. Layout guides do not define a new view.
They do not participate in the view hierarchy.
Instead, they simply define a rectangular region in their owning view’s coordinate system that can interact with Auto Layout.
打印layoutConstraints:
[
<NSLayoutConstraint:0x7fb6ebb0f050 H:|-(10)-[UIImageView:0x7fb6e9ef3280] (Names: '|':UITableViewCellContentView:0x7fb6ebc02dc0 )>,
<NSLayoutConstraint:0x7fb6ebb0f0a0 V:[UIImageView:0x7fb6e9ef3280]-(10)-| (Names: '|':UITableViewCellContentView:0x7fb6ebc02dc0 )>,
<NSLayoutConstraint:0x7fb6ebb0f0f0 V:|-(10)-[UIImageView:0x7fb6e9ef3280] (Names: '|':UITableViewCellContentView:0x7fb6ebc02dc0 )>,
<NSLayoutConstraint:0x7fb6ebb0f140 UITableViewCellContentView:0x7fb6ebc02dc0.trailingMargin >= UILabel:0x7fb6ebb092c0'<nameLabel>'.trailing>,
<NSLayoutConstraint:0x7fb6ebb0f190 UILabel:0x7fb6ebb092c0'<nameLabel>'.top == UITableViewCellContentView:0x7fb6ebc02dc0.topMargin + 15>,
<NSLayoutConstraint:0x7fb6ebb0f1e0 H:[UIImageView:0x7fb6e9ef3280]-(10)-[UILabel:0x7fb6ebb092c0'<nameLabel>']>,
<NSLayoutConstraint:0x7fb6ebb0f230 V:[UILabel:0x7fb6ebb092c0'<nameLabel>']-(0)-[UILabel:0x7fb6ebb0d510'<locationNameLabel>']>,
<NSLayoutConstraint:0x7fb6ebb0f280 UILabel:0x7fb6ebb0d510'<locationNameLabel>'.leading == UILabel:0x7fb6ebb092c0'<nameLabel>'.leading>,
<NSLayoutConstraint:0x7fb6eb821de0 UILayoutGuide:0x7fb6eb82b020''.top == UILabel:0x7fb6ebb092c0'<nameLabel>'.top>,
<NSLayoutConstraint:0x7fb6eb821ef0 UILayoutGuide:0x7fb6eb82b020''.bottom == UILabel:0x7fb6ebb0d510'<locationNameLabel>'.bottom>,
<NSLayoutConstraint:0x7fb6eb823d40 UILayoutGuide:0x7fb6eb82b020''.centerY == UITableViewCellContentView:0x7fb6ebc02dc0.centerY>
]
Placeholder constraint : storyboard上用,运行时删除掉。