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上 ...
随机推荐
- JAVA后端常用框架SSM,redis,dubbo等
JAVA后端常用框架SSM,redis,dubbo等 一.SpringMVC http://blog.csdn.net/evankaka/article/details/45501811 spri ...
- Spring boot 整合jsp、thymeleaf、freemarker
1.创建spring boot 项目 2.pom文件配置如下: <dependencies> <dependency> <groupId>org.springfra ...
- 零基础学习 Python 之字符串
初识字符串 维基百科对于字符串的定义式:字符串是由零个或者多个字符组成的有限串行.你之前学会敲的第一行 print 代码里的 "Hello World",就是一个字符串.字符串的本 ...
- maven学习(十二)——maven聚合与继承实战
聚合与继承实战 创建四个Maven项目,如下图所示:
- 【bzoj4439】[Swerc2015]Landscaping 网络流最小割
题目描述 FJ有一块N*M的矩形田地,有两种地形高地(用‘#’表示)和低地(用‘.’表示) FJ需要对每一行田地从左到右完整开收割机走到头,再对每一列从上到下完整走到头,如下图所示 对于一个4*4的田 ...
- 【Luogu】P2488工作安排(费用流)
题目链接 这题……费用流即可……(哇啊要被打死辣) 然而我printf("%d")爆零四次 好的心如死灰 #include<cstdio> #include<cs ...
- HDU 4391 Paint The Wall(分块+延迟标记)
Paint The Wall Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- js确保正确this的几种写法
1.直接用bind调用 this.method.bind(this) 2.构造函数中用bind定义 class Foo{ constructor(){ this.method = this.metho ...
- docker (centOS 7) 使用笔记5 - weave网络
weave官网 https://www.weave.works 1. 下载安装 sudo curl -L git.io/weave -o /usr/local/bin/weave sudo chmod ...
- docker 集群 kubernetes 1.8 构建
1.环境说明:操作系统:CentOS7Kubernetes 版本:v1.8.3Docker 版本:v17.06-ce master 192.168.10.220 etcd kube-apiser ...