【iOS】关于 UIView 的 layoutSubviews 方法

UIKit 的 UIView 是一个非常重要的类,几乎每个尝试 iOS 开发的程序员都会用到它。UIView 本身实现了Composite Pattern,所以一个应用的界面最终可以由一群树状组合的 UIView 来组合而成——在这棵 UIView 树的最顶部,是继承于 UIView 的 UIWindow 实例,然后是由 UIWindow 实例保有的 rootViewController 的根 UIView 实例,然后是在该 UIView 实例上的各种各样的子节点 UIView。

父 UIView 可以拥有自己的子 UIView,自然而然的,父 UIView 就会面对用怎样的策略来布局、排列这些子 UIView 的问题。在 UIView 中,UIKit 的开发者专门提供了layoutSubviews方法来解决这个问题。

官方文档的描述

官方文档对于该方法有如下的描述:

(This method) Lays out subviews.

Subclasses can override this method as needed to perform more precise layout of their subviews.You should override this method only if the autoresizing and constraint-based behaviors of the subviews DO NOT offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly.

以上节选自UIView Class Reference

Whenever the size of a view changes, UIKit applies the autoresizing behaviors of that view’s subviews andTHENcalls the layoutSubviews method of the view to let it make manual changes. You can implement the layoutSubviews method in custom views when the autoresizing behaviors by themselvesDO NOTyield the results you want. Your implementation of this method can do any of the following:

Adjust the size and position of any immediate subviews.

Add or remove subviews or Core Animation layers.

Force a subview to be redrawn by calling its setNeedsDisplay or setNeedsDisplayInRect: method.

One place where applications often lay out subviews manually is when implementing a large scrollable area. Because it is impractical to have a single large view for its scrollable content, applications often implement a root view that contains a number of smaller tile views. Each tile represents a portion of the scrollable content. When a scroll event happens, the root view calls its setNeedsLayout method to initiate a layout change. Its layoutSubviews method then repositions the tile views based on the amount of scrolling that occurred. As tiles scroll out of the view’s visible area, the layoutSubviews method moves the tiles to the incoming edge, replacing their contents in the process.

以上节选自View Programming Guide for iOS

从文档的描述可以看到,layoutSubviews 的主要功能就是让程序员自己实现子 UIViews 的布局算法,从而在需要重新布局的时候,父 UIView 会按照这个流程重新布局自己的子 UIViews。而且,layoutSubviews 方法只能被系统触发调用,程序员不能手动直接调用该方法。要引起该方法的调用,可以调用 UIView 的setNeedsLayout方法来标记一个 UIView。这样一来,在 UI 线程的下次绘制循环中,系统便会调用该 UIView 的 layoutSubviews 方法。

使用 layoutSubviews 的实例

一个比较典型的例子来自于RDVTabBarController项目,它的目标是实现一个提供高定制性的 TabBarController。在这个项目中,作者使用了 layoutSubviews 来控制 TabBar 的子 UIView——TabBarItem 的重新布局,从而达到 TabBar 发生变化时 TabBarItem 的绘制与布局不会出错的目的:



我在代码中的注释解释了这段代码都做了什么。如果你要实现自己的 layoutSubviews 方法的话,可以参考这个例子的流程。

何时被调用

一个曾经让我比较疑惑的问题是,既然我不能手动直接调用该方法,那在什么时候、何种条件下这个方法会被调用呢?

Stackoverflow 上已经有相关的讨论了(作者在他的博客上有更详细的描述),并且有一位朋友给出了很不错的解答:

init does not cause layoutSubviews to be called (duh)

addSubview causes layoutSubviews to be called on the view being added, the view it’s being added to (target view), and all the subviews of the target

view setFrame intelligently calls layoutSubviews on the view having its frame set only if the size parameter of the frame is different

scrolling a UIScrollView causes layoutSubviews to be called on the scrollView, and its superview

rotating a device only calls layoutSubview on the parent view (the responding viewControllers primary view)

Resizing a view will call layoutSubviews on its superview

也就是说,layoutSubviews 方法会在这些情况下,在这些 UIView 实例上被调用:

addSubview 被调用时:target view(一定会),以及被添加的 view(第一次调用会)

更改 UIView 的 frame 时:被更改 frame 的 view(frame 与之前不同时)

对于 UIScrollView 而言,滚动式:scroll view

设备的 orientation 改变时:涉及改变的 UIViewController 的 root view

使用 CGAffineTransformScale 改变 view 的 transform 属性时,view 的 superview:被改变的 view

然而,根据我自己的实验,上面的描述并不是很完善的。我的两点补充如下:

第一次调用 addSubview 的时候,target view 和被添加到 target view 的 view 的 layoutSubviews 方法会被调用。在已经添加完毕后,若 target view 已经拥有该被添加 view,则只有 target view 的 layoutSubviews 方法会被调用。“and all the subviews of the target”这句话是错误的。

只有 UIView 处于 key window 的 UIView 树中时,该 UIView 的 layoutSubviews 方法才有可能被调用。不在树中的不会被调用。这也是为什么 Stackoverflow 上的讨论中这个答案的第二点会被提出。

小结

使用 layoutSubviews 可以让应用界面的适应能力更强。如果 UIKit 默认提供的自动布局机制Auto Layout不能提供给你想要的 UIView 布局行为,你可以自己定制该方法来决定布局行为。



转自 关于 UIView 的 layoutSubviews 方法 - 巴赫在编码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,393评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,790评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,391评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,703评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,613评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,003评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,507评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,158评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,300评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,256评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,274评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,984评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,569评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,662评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,899评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,268评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,840评论 2 339

推荐阅读更多精彩内容