Beginning Auto Layout Tutorial in iOS 7: Part 4
A little runtime excursion
为两个button都添加同一个ibaction方法在viewcontroller.m中实现如下的方法:

不论怎么变化,都会有如下的规律
- The lower button is always center-aligned in the window, horizontally. 下方的button总是水平居中显示
- The lower button always sits 20 points from the bottom of the window. 总是和底部保持特定的距离大小
- The top button is always left-aligned with the lower button and 40 points above it. 左上方的button总是和下方的button左对齐并且总是保持特定间距
再次测试,删除Leading Alignment constraint,在align中选择Right Edges然后再运行看看区别。
然后可以选择Align\Horizontal Centers再次尝试。
Fixing the width
选择两个button然后选择Editor\Pin\Widths Equally

然后把下面的button的内容变成“X”,那么你会发现上面的button的显示不再正确了

那么auto layout又是怎么知道该使用哪个button的大小呢?选择Size to Fit Content应用到上面的button,运行app,两个button还是有相同的宽度,不论内容实际大小

运行app会发现两个button总是一样宽,不论哪个文本更宽些。当然当文本非常短时两个也一样短。除非一个约束来制约他们否则,buttons会很好的适应当前他们显示的内容,这个就是intrinsic content size
Intrinsic Content Size
Before Auto Layout, you always had to tell buttons and other controls how big they should be, either by setting their frame or bounds properties or by resizing them in Interface Builder. But it turns out that most controls are perfectly capable of determining how much space they need, based on their content.
A label knows how wide and tall it is because it knows the length of the text that has been set on it, as well as the font size for that text. Likewise for a button, which might combine the text with a background image and some padding.
The same is true for segmented controls, progress bars, and most other controls, although some may only have a predetermined height but an unknown width.
This is known as the intrinsic content size, and it is an important concept in Auto Layout. You have already seen it in action with the buttons. Auto Layout asks your controls how big they need to be and lays out the screen based on that information.
Usually you want to use the intrinsic content size, but there are some cases where you may not want to do that. You can prevent this by setting an explicit Width or Height constraint on a control.
Imagine what happens when you set an image on a UIImageView if that image is much larger than the screen. You usually want to give image views a fixed width and height and scale the content, unless you want the view to resize to the dimensions of the image.
选择Pin\Width可以复写他自带的Intrinsic Content Size功能。

Attributes inspector. Change Constant to 80,运行app会发现如下的情况。因为两个button的宽度要一致。但是上面的指定了固定的宽度,导致了即便文字宽度改变了但是button的宽度缺无法再变化了。

Note: You probably wouldn’t set a Width constraint on a button by design – it is best to let the button use its intrinsic size – but if you ever run into a layout problem where you expect your controls to change size and they don’t, then double check to make sure a fixed Width constraint didn’t sneak in there.
Beginning Auto Layout Tutorial in iOS 7: Part 4的更多相关文章
- Beginning Auto Layout Tutorial in iOS 7: Part 3
How Auto Layout works 在使用auto layout之前,你可能总是使用initWithFrame或者frame, bounds or center属性. 使用约束的好处在于你不需 ...
- Beginning Auto Layout Tutorial in iOS 7: Part 6
Gallery example 屏幕有四个分开的相同的矩形,每个矩形有一个label和一个image view.创建一个Gallery的项目.在Main.storyboard中,拖拉一个view大小为 ...
- Beginning Auto Layout Tutorial in iOS 7: Part 1
可以更好的结局屏幕方向和兼容iphone和ipad的解决方案. iOS6有一个新的技术auto layout来帮助解决这个问题.这个技术不仅可以支持app不同尺寸下的开发,而且你也不需要为每一种语言创 ...
- Beginning Auto Layout Tutorial in iOS 7: Part 2
Auto Layout to the rescue! 接下来就看看如何使用Auto Layout来实现这个效果. 首先移除viewWillLayoutSubviews方法,选择Main.storybo ...
- Swift语言Auto Layout入门教程:上篇
原文:Beginning Auto Layout Tutorial in Swift: Part 1/2,译者:@TurtleFromMars 开始用自动布局约束的方式思考吧! 更新记录:该教程由Br ...
- iOS布局之Auto Layout
学习资源: <iOS6核心编程>自动布局部分 <iOS6范例经典>自动布局部分 Tutorial: iOS 6 Auto Layout versus Springs and S ...
- How to Use Auto Layout in XCode 6 for iOS 7 and 8 Development
The Auto Layout is available on the Storyboard for iOS or OS X development since XCode 5. But, I did ...
- iOS Programming Auto Layout: Programmatic Constraints 自动布局:通过编程限制
iOS Programming Auto Layout: Programmatic Constraints 1. However, if your views are created in co ...
- 【转】使用 Auto Layout 的典型痛点和技巧
layoutIfNeeded()强制立刻更新布局 原文网址:http://www.jianshu.com/p/0f031606e5f2 官方文档:Auto Layout Guide 加上去年WWDC上 ...
随机推荐
- 使用html进行浏览器判断,浏览器条件注释
下面来点今天写东西的时候查资料,收集的关于使用html进行浏览器判断的一些资料: 条件注释的基本格式: <!--[if expression]>注释内容<![endif]--> ...
- 【4Sum】cpp
题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- CSU-2173 Use FFT
CSU-2173 Use FFT Description Bobo computes the product P(x)⋅Q(x)=\(c_0 + c_1x + - + c_{n+m}x^{n + m} ...
- selenium webdriver——控制浏览器
from selenium import webdriver import time def controlBrowser(): #启动浏览器 driver = webdriver.Firefox() ...
- 【bzoj4386】[POI2015]Wycieczki 矩阵乘法
题目描述 给定一张n个点m条边的带权有向图,每条边的边权只可能是1,2,3中的一种.将所有可能的路径按路径长度排序,请输出第k小的路径的长度,注意路径不一定是简单路径,即可以重复走同一个点. 输入 第 ...
- 并查集:按秩合并 $n$ 个点所得树高不超过 $\lfloor\log n \rfloor$
用 $h_n$ 表示按秩合并 $n$ 个点所得树的最大高度. 有 $h_1 = 0, h_2 = 1, h_3 = 1, h_4 = 2, h_5 = 2, \dots$ 有如下地推: \[ h_n ...
- cf 853 B Jury Meeting [前缀和]
题面: 传送门 思路: 看完题目以后,首先有一个结论:每个人都是先去到首都,等待开会,开会结束以后再一个个走掉 而且这道题只有去首都和离开首都的机场 因此考虑计算去首都的飞机的前缀最小花费,以及离开首 ...
- mongodb学习(2)--- nodeJS与MongoDB的交互(使用mongodb/node-mongodb-native)
转载:http://www.cnblogs.com/zhongweiv/p/node_mongodb.html 目录 简介 MongoDB安装(windows) MongoDB基本语法和操作入门(mo ...
- callee返回正被执行的Function对象
arguments.length是实参长度, arguments.callee.length是形参长度. function fn(a, b, c, d) { console.log(arguments ...
- 负载平衡(cogs 741)
«问题描述:G 公司有n 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使n 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运.«编程任务:对于给定的n 个环 ...