首页 > 代码库 > viewDidLayoutSubviews 与 layoutSubviews 调用顺序

viewDidLayoutSubviews 与 layoutSubviews 调用顺序

了解演示demo的UI布局:

(lldb) po [[self view] recursiveDescription]

<UIView: 0x8d8f070; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x8d8f490>>

   | <ZQCustomView: 0x8c61130; frame = (0 0; 0 0); layer = <CALayer: 0x8c61220>>

   |    | <UIButton: 0x8f55530; frame = (100 100; 50 50); opaque = NO; layer = <CALayer: 0x8f59320>>

   |    |    | <UIButtonLabel: 0x8f594e0; frame = (9 16; 32 18); text = ‘ GO ‘; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8f595d0>>


也即是:self.view 上添加个自定义的customView


打印调用的顺序:

TestFunctionCall[49445:60b] ---> -[ZQRootViewController viewDidLoad]

TestFunctionCall[49445:60b] ---> -[ZQCustomView initWithFrame:]

TestFunctionCall[49445:60b] ---> -[ZQRootViewController viewWillLayoutSubviews]

TestFunctionCall[49445:60b] ---> -[ZQRootViewController viewDidLayoutSubviews]

TestFunctionCall[49445:60b] ---> -[ZQCustomView layoutSubviews]

TestFunctionCall[49445:60b] ---> -[ZQCustomView layoutSubviews]

TestFunctionCall[49445:60b] ---> -[ZQCustomView drawRect:]


先调用试图控制器的viewWillLayoutSubviews 以及viewDidLayoutSubviews,然后调用view的layoutSubviews、layoutSubviews 。最后调用drawRect


viewDidLayoutSubviews 与 layoutSubviews 调用顺序