UIStackView

iOS9在布局方面最大的变化就是引入了UIStackView.那么它是什么呢?简单讲就是一个容器里可以包含多个控件,分为水平和竖直排列.我们只需约束这个容器即可,而不用一个个地去约束容器内的控件.而且这个容器是可以嵌套的.如果你接触过Watch开发,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,已经很轻松的完成了一个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

关于Alignment的属性,水平和竖直时有区别:

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 anchorslayout 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
)

现在可以简化为:

    let constraint = topLabel.bottomAnchor.constraintEqualToAnchor(
          bottomLabel.topAnchor, constant: 8)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 作者:Umberto Raimondi,原文链接,原文日期:2015-12-08译者:CoderAFI;校对:Ch...
    梁杰_numbbbbb阅读 1,306评论 0 7
  • 前言 首先,我们通过下面这张图片引出今天的主角 大家看到了什么,是爱吗?不,这不是爱,不是爱,是满满的‘愁绪’?😂...
    一念之见阅读 2,223评论 0 2
  • 这是一篇挺老的文章,主要就是介绍在iOS9时推出的控件UIStackView。我发现网上的资料并不算多,而AppC...
    Liberalism阅读 11,148评论 2 26
  • 距离iOS9发布已经接近一年了,我们即将引来新的iOS 10,为何在这个时候来介绍iOS9中新引入的一个布局组件呢...
    CZ_iOS阅读 7,560评论 9 59
  • 该文章属于刘小壮原创,转载请注明:刘小壮[https://www.jianshu.com/u/2de707c93d...
    刘小壮阅读 15,597评论 5 73