Beginning Auto Layout Tutorial in iOS 7: Part 4

A little runtime excursion

为两个button都添加同一个ibaction方法在viewcontroller.m中实现如下的方法:

Short and long titles

不论怎么变化,都会有如下的规律

  • 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

Buttons with the Equal Widths constraint

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

Top button text no longer fits

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

Equal widths buttons in app

运行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功能。

Button with Fixed Width constraint

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

Button text clipped

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.

 

posted @ 2014-12-29 15:54  如来藏  阅读(202)  评论(0编辑  收藏  举报