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的更多相关文章

  1. Beginning Auto Layout Tutorial in iOS 7: Part 3

    How Auto Layout works 在使用auto layout之前,你可能总是使用initWithFrame或者frame, bounds or center属性. 使用约束的好处在于你不需 ...

  2. Beginning Auto Layout Tutorial in iOS 7: Part 6

    Gallery example 屏幕有四个分开的相同的矩形,每个矩形有一个label和一个image view.创建一个Gallery的项目.在Main.storyboard中,拖拉一个view大小为 ...

  3. Beginning Auto Layout Tutorial in iOS 7: Part 1

    可以更好的结局屏幕方向和兼容iphone和ipad的解决方案. iOS6有一个新的技术auto layout来帮助解决这个问题.这个技术不仅可以支持app不同尺寸下的开发,而且你也不需要为每一种语言创 ...

  4. Beginning Auto Layout Tutorial in iOS 7: Part 2

    Auto Layout to the rescue! 接下来就看看如何使用Auto Layout来实现这个效果. 首先移除viewWillLayoutSubviews方法,选择Main.storybo ...

  5. Swift语言Auto Layout入门教程:上篇

    原文:Beginning Auto Layout Tutorial in Swift: Part 1/2,译者:@TurtleFromMars 开始用自动布局约束的方式思考吧! 更新记录:该教程由Br ...

  6. iOS布局之Auto Layout

    学习资源: <iOS6核心编程>自动布局部分 <iOS6范例经典>自动布局部分 Tutorial: iOS 6 Auto Layout versus Springs and S ...

  7. 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 ...

  8. iOS Programming Auto Layout: Programmatic Constraints 自动布局:通过编程限制

    iOS Programming  Auto Layout: Programmatic Constraints  1.  However, if your views are created in co ...

  9. 【转】使用 Auto Layout 的典型痛点和技巧

    layoutIfNeeded()强制立刻更新布局 原文网址:http://www.jianshu.com/p/0f031606e5f2 官方文档:Auto Layout Guide 加上去年WWDC上 ...

随机推荐

  1. java中equals和==

    https://www.cnblogs.com/bluestorm/archive/2012/03/02/2377615.html

  2. Jmeter随笔一

    资料分享:http://www.cnblogs.com/yangxia-test/p/3964881.html

  3. PostgreSQL 数组类型

    PostgreSQL 支持表的字段使用定长或可变长度的一维或多维数组,数组的类型可以是任何数据库内建的类型.用户自定义的类型.枚举类型, 以及组合类型.但目前还不支持 domain 类型. 数组类型的 ...

  4. iOS开发UI篇—自定义layer

    一.第一种方式 1.简单说明 以前想要在view中画东西,需要自定义view,创建一个类与之关联,让这个类继承自UIView,然后重写它的DrawRect:方法,然后在该方法中画图. 绘制图形的步骤: ...

  5. java的图像界面

    package test; import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.even ...

  6. android2.2 watchdog分析

    1 watchdog分析 Watchdog就是“看门狗”.其最初存在的意义是因为以前嵌入式设备上的程序经常跑飞(电磁干扰之类的),所以专门设置了一个硬件看门狗,每个一段时间,看门狗就去检查一下某个参数 ...

  7. vue2搭建简易spa

    使用vue-cli来配置webpack,webpack是一个打包工具,使程序模块化 全局安装vue-cli: npm install -g vue-cli 安装好后,使用vue-cli脚手架配置web ...

  8. Crash的数字表格(bzoj 2054)

    Description 今天的数学课上,Crash小朋友学习了最小公倍数(Least Common Multiple).对于两个正整数a和b,LCM(a, b)表示能同时被a和b整除的最小正整数.例如 ...

  9. hihocoder #1407 : 后缀数组二·重复旋律2

    #1407 : 后缀数组二·重复旋律2 Time Limit:5000ms Case Time Limit:1000ms Memory Limit:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢 ...

  10. c++基础(一)

    c++中静态内存分配与动态内存分配:1.静态对象是有名字的变量, 我们直接对其进行操作 ,而动态对象是没有名字的变量我们通过指针间接地对它进行操作.(静态内存分配是因为在源码编译的时候,编译器就为变量 ...