博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Stack views
阅读量:5079 次
发布时间:2019-06-12

本文共 5083 字,大约阅读时间需要 16 分钟。

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 之后

 

Note: A view doesn't have an anchor property for every possible layout attribute. Any of the attributes relating to the margins, such as .TopMargin,
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).
The layout guide has all the same anchor properties as the view, but now they relate to the view's margins, for example .
TopMargin would be represented by layoutMarginsGuide.topAnchor. 
 

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上用,运行时删除掉。

转载于:https://www.cnblogs.com/cocoabanana/p/5567630.html

你可能感兴趣的文章
编写一个函数isMerge,判断一个字符串str是否可以由其他两个字符串part1和part2“组合”而成...
查看>>
一个自己写的判断2个相同对象的属性值差异的工具类
查看>>
oracle连接的三个配置文件(转)
查看>>
Python内置函数(29)——help
查看>>
Android TextView加上阴影效果
查看>>
《梦断代码》读书笔记(三)
查看>>
AngularJS学习篇(一)
查看>>
关于Xshell无法连接centos6.4的问题
查看>>
spring security 11种过滤器介绍
查看>>
代码实现导航栏分割线
查看>>
大数据学习系列(8)-- WordCount+Block+Split+Shuffle+Map+Reduce技术详解
查看>>
luogu4849 寻找宝藏 (cdq分治+dp)
查看>>
关于源程序到可运行程序的过程
查看>>
C# Async与Await的使用
查看>>
Mysql性能调优
查看>>
iOS基础-UIKit框架-多控制器管理-实例:qq界面框架
查看>>
poj1611 简单并查集
查看>>
Ubuntu 14.04下安装CUDA8.0
查看>>
跨平台开发 -- C# 使用 C/C++ 生成的动态链接库
查看>>
C# BS消息推送 SignalR介绍(一)
查看>>