iOS9在布局方面最大的變化就是引入了UIStackView.那么它是什么呢享完?簡單講就是一個容器里可以包含多個控件,分為水平和豎直排列.我們只需約束這個容器即可有额,而不用一個個地去約束容器內(nèi)的控件.而且這個容器是可以嵌套的.如果你接觸過Watch開發(fā),UIStackView有點像其中的Group控件.那就隨我去瞅瞅它是什么吧!
首先選中三個UIButton:
Screen Shot 2015-10-27 at 9.15.48 PM.png
Screen Shot 2015-10-27 at 9.16.54 PM.png
然后點一下約束左邊的那個有箭頭的東西:
Screen Shot 2015-10-27 at 9.17.49 PM.png
ok,已經(jīng)很輕松的完成了一個UIStackView!
Screen Shot 2015-10-27 at 9.18.57 PM.png
然后只要約束一下這個UIStackView就可以了.
Screen Shot 2015-10-27 at 9.21.16 PM.png
我們可以看到這個UIStackView有幾個屬性:三個按鈕水平并排般又,可以看到Axis的屬性為Horizontal,即軸線屬性為水平.Distribution屬性為Equal Spacing即相隔相同距離.All Done.很容易就完成了這三個按鈕的布局.而如果按照以往,你可能需要做很多約束:(
Screen Shot 2015-10-27 at 9.27.24 PM.png
關(guān)于Alignment的屬性,水平和豎直時有區(qū)別:
Screen Shot 2015-10-27 at 9.59.19 PM.png
水平時各個屬性的效果:
Screen Shot 2015-10-27 at 10.00.56 PM.png
Screen Shot 2015-10-27 at 10.01.15 PM.png
豎直時各個屬性的效果:
Screen Shot 2015-10-27 at 10.01.51 PM.png
AutoLayout還有兩個新物件layout anchors和layout guides
- Layout anchors
當我們有兩個UILabel,bottomLabel和topLabel,你想把bottomLabel放在topLabel右邊間隔8 points的位置,以前你需要添加如下約束:
let constraint = NSLayoutConstraint(
item: topLabel,
attribute: .Bottom,
relatedBy: .Equal,
toItem: bottomLabel,
attribute: .Top,
multiplier: 1,
constant: 8
)
現(xiàn)在可以簡化為:
let constraint = topLabel.bottomAnchor.constraintEqualToAnchor(
bottomLabel.topAnchor, constant: 8)