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 and THEN calls 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 themselves DO NOT yield 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 主要是为了让我们去实现UIView的策略来布局以及排列等,这样就可以保证我们在需要新的布局方案的时候,父类UIView 会按照我们制定的方案去布局。我们在某个类的内部调整子视图位置时,需要调用。反过来的意思就是说:如果你想要在外部设置 sub views 的位置,就不要重写。而且,layoutSubviews 方法只能被系统触发调用,不可以手动去调用。 要引起该方法的调用,可以调用 UIView 的 setNeedsLayout 方法来标记一个 UIView。这样一来,在 UI 线程的下次绘制循环中,系统便会调用该 UIView 的 layoutSubviews 方法。
在苹果的官方文档中强调:
You should override this method only if the autoresizing behaviors of the subviews do not offer the behavior you want.
layoutSubviews, 当我们在某个类的内部调整子视图位置时,需要调用;如果你想要在外部设置subviews的位置,就不要重写。
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
翻译:
- init初始化不会触发layoutSubviews 但是是用initWithFrame 进行初始化时,当rect的值不为CGRectZero时,也会触发
- addSubview会触发layoutSubviews
- 设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化
- 滚动一个UIScrollView会触发layoutSubviews
- 旋转Screen会触发父UIView上的layoutSubviews事件
- 改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件
iOS 中 layout 的相关方法:
- layoutSubviews
- layoutIfNeeded
- setNeedsLayout
- setNeedsDisplay
- drawRect
- sizeThatFits
- sizeToFit
当然这并不齐全。
1. setNeedsLayout方法:
标记为需要重新布局,异步调用layoutIfNeeded刷新布局,不立即刷新,但layoutSubviews一定会被调用
2. layoutIfNeeded方法:
如果有需要刷新的标记,立即调用layoutSubviews进行布局;如果没有标记,不会调用layoutSubviews;如果要立即刷新,要先调用[view setNeedsLayout],把标记设为需要布局,然后马上调用[view layoutIfNeeded],实现布局。在视图第一次显示之前,标记总是“需要刷新”的,可以直接调用[view layoutIfNeeded]
3. 重绘
drawRect方法:重写此方法,执行重绘任务
setNeedsDisplay方法:标记为需要重绘,异步调用drawRect
setNeedsDisplayInRect方法:标记为需要局部重绘
4. sizeToFit
sizeToFit会自动调用sizeThatFits方法;
sizeToFit不应该在子类中被重写,应该重写sizeThatFits
sizeThatFits传入的参数是receiver当前的size,返回一个适合的size
sizeToFit可以被手动直接调用
sizeToFit和sizeThatFits方法都没有递归,对subviews也不负责,只负责自己
Tips:
layoutSubviews对subviews重新布局
layoutSubviews方法调用先于drawRect
setNeedsLayout在receiver标上一个需要被重新布局的标记,在系统runloop的下一个周期自动调用layoutSubviews
layoutIfNeeded方法如其名,UIKit会判断该receiver是否需要layout.根据Apple官方文档,layoutIfNeeded方法应该是这样的
layoutIfNeeded遍历的不是superview链,应该是subviews链
drawRect是对receiver的重绘,能获得context
setNeedDisplay在receiver标上一个需要被重新绘图的标记,在下一个draw周期自动重绘,iphone device的刷新频率是60hz,也就是1/60秒后重绘