基础
官方WWDC的基础教学视频:
202 – Introduction to Auto Layout for iOS and OS X https://developer.apple.com/videos/wwdc/2012/?id=202,
228 – Best Practices for Mastering Auto Layout https://developer.apple.com/videos/wwdc/2012/?id=228,
232 – Auto Layout by Example https://developer.apple.com/videos/wwdc/2012/?id=232约束条件是用的可视格式语言,官方文档:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/VisualFormatLanguage/VisualFormatLanguage.html
原理
视图显示前会有两个步骤,顺序是updating constraints -> laying out views -> 显示。
Updating constraints:从子视图到父视图,布局会在实际设置frame时使用,调用setNeedsUpdateConstraints触发操作。自定义视图的话可以重写updateConstraints增加本地约束。
Laying out views:布局视图是从父视图到子视图,通过setNeedsLayout触发。调用layoutIfNeeded可以强制系统立刻更新视图布局。
自定义视图自动布局的过程
Instrinsic Content Size
实现Instrinsic Content Size需要重写intrinsicContentSize返回合适的大小,有会影响尺寸改变的时候调用invalidateInstrinsicContentSize。一个方向设置Instrinsic Content Size,另一个方向尺寸返回UIViewNoIntrinsicMetric
Compression Resistance 和 Content Hugging
定义了Instrinsic Content Size 才能够在视图两个方向上分配 Compression Resistance 和 Content Hugging 。比如一个Instrinsic Content Size为{100,30}的label,Compression Resistance为750,Content Hugging为250,约束条件可视格式语言如下
1 |
|
Frame和Alignment Rect
如果需要可以重写alignmentRectForFrame:和frameForAlignmentRect:,Instrinsic Content Size尺寸引用它的alignment rect而不是frame
Baseline Alignment
通过viewForBaselineLayout来激活基线对齐。
控制布局
本地约束:添加本地约束的地方是updateConstraints。增加布局子视图约束条件后调用[super updateConstraints]。
控制子视图布局:如果不能利用布局约束条件达到子视图预期布局可以重写layoutSubviews。可以参看WWDC视频的一个例子WWDC session 228 – Best Practices for Mastering Auto Layout http://onevcat.com/2012/09/autoayout/
1 | - layoutSubviews |
对于不固定高度的多行文本处理
比如说UILabel和NSTextField文本的高度取决于行的宽度,这两个类有个perferredMaxLayoutWidth的属性,可以指定行宽度的最大值,以便计算固有内容尺寸。
1 | - (void)layoutSubviews |
动画
- 使用Core Animation方法
//非Auto Layout的写法
1 | [UIView animateWithDuration:1 animations:^{ |
使用transform来产生动画,将这个view嵌入到一个view的容器内,然后在这个容器内重写layoutSubviews
1 | - (void)layoutSubviews |
调试
不可满足的约束条件
遇到不可满足的约束条件只能在输入的日志中看到视图的内存地址。
1 | (lldb) po 0x7731880 |
可以在控制台修改有问题的视图
1 | (lldb) expr ((UIView *)0x7731880).backgroundColor = [UIColor purpleColor] |
这里有Danielhttps://twitter.com/danielboedewadt的一个调试Auto Layout的范例:https://github.com/objcio/issue-3-auto-layout-debugging
有歧义的布局
UIView提供三种方法:hasAmbiguousLayout,exerciseAmbiguityInLayout和_autolayoutTrace(私有方法,正式产品里不要包含)。如果有歧义那么hasAmbiguousLayout返回YES。
1 | @implementation UIView (AutoLayoutDebugging) |
_autolayoutTrace打印如下:
1 | 2013-07-23 17:36:08.920 FlexibleLayout[4237:907] |
使用exerciseAmbiguityInLayout
1 | @implementation UIView (AutoLayoutDebugging) |
约束条件代码
可视化结构语言(visual format language, VFL)官方文档:
http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/translatesAutoresizingMaskIntoConstraints如果是使用代码设置约束,需要将translatesAutoResizingMaskIntoConstraints 设置为NO。
constraintsWithVisualFormat:options:metrics:views:方法的option参数可以允许调整一定范围内的view,不同于格式化字符只能影响一个view。比如使用NSLayoutFormatAlignAllTop排列可视化语言里所有view为上边缘对其。
父视图中居中子视图的技巧,利用了不均等约束和可选参数。下面代码在父视图中水平排列了一个视图
1 | UIView *superview = theSuperView; |
垂直的排列一系列view,想要它们垂直方向间距一致,水平方向上所有view以他们的左边缘对齐
1 | @implementation UIView (AutoLayoutHelpers) |