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上 ...
随机推荐
- NOS直传加速服务
本文来自网易云社区 作者:孙建良 最近团队在对存储系统做一些性能测试,期间遇到了不少问题,测试过程中得出的结果也没有很好的数据支撑,所以尝试了非常多的方法来对性能问题进行定位. 小王童鞋是挺厉害的,使 ...
- leetcode 【 Remove Element 】python 实现
题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...
- python - 接口自动化测试 - TestLogin - 登录接口测试用例
# -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: test_login.py @ide: PyCharm C ...
- PostgreSQL 数组类型
PostgreSQL 支持表的字段使用定长或可变长度的一维或多维数组,数组的类型可以是任何数据库内建的类型.用户自定义的类型.枚举类型, 以及组合类型.但目前还不支持 domain 类型. 数组类型的 ...
- CSU-1163 寒衣调
CSU-1163 寒衣调 Description 男从戎,女守家.一夜,狼烟四起,男战死沙场.从此一道黄泉,两地离别.最后,女终于在等待中老去逝去.逝去的最后是换尽一生等到的相逢和团圆. 某日两人至奈 ...
- hnust CZJ-Superman
问题 B: CZJ-Superman 时间限制: 1 Sec 内存限制: 128 MB提交: 636 解决: 87[提交][状态][讨论版] 题目描述 “那是只鸟?那是飞机?那是——超人!” 程序 ...
- 非旋Treap总结 : 快过Splay 好用过传统Treap
非旋$Treap$ 其高级名字叫$Fhq\ Treap$,既然叫$Treap$,它一定满足了$Treap$的性质(虽然可能来看这篇的人一定知道$Treap$,但我还是多说几句:$Fhp\ Treap$ ...
- Box 类
public class Box extends JComponent implements Accessible使用 BoxLayout 对象作为其布局管理器的一个轻量级容器.Box 提供几个对使用 ...
- 用最优方法从LinkedList列表中删除重复元素
用运行速度最优的方法从LinkedList列表里删除重复的元素,例如A->B->BB->B->C,返回A->B->BB->C. 考试的时候没完全想明白,考完又 ...
- POJ 3368:Frequent values(线段树区间合并)
题目大意,给出一段非降序列,求一些区间中出现频率最高的数的出现次数. 分析: 显然,区间中一个数多次出现必然是连续的,也就是最长的连续相等的一段. 用线段树解决,维护三个信息:一个区间最长连续的区间的 ...