Apple doc regarding viewDidLayoutSubviews
says:
When the bounds change for a view controller's view, the view adjusts the positions of its subviews and then the system calls this method. However, this method being called does not indicate that the individual layouts of the view's subviews have been adjusted. Each subview is responsible for adjusting its own layout.
Your view controller can override this method to make changes after the view lays out its subviews. The default implementation of this method does nothing.
I have view1
which contains view2
means view2
is a subview of view1
. I am creating a CALayer
for my view2
which I want to be exact size of view2
. I am using auto layouts
so I want to create the CALayer
when auto layout
finish its work so I can have correct values to set for CALayer
frame.
But in viewDidLayoutSubviews
method I can't detect when exactly view2
frame is set by auto layout
because Apple doc is also saying that
this method being called does not indicate that the individual layouts of the view's subviews have been adjusted.
after that Apple doc saying
Your view controller can override this method to make changes after the view lays out its subviews.
I am so confused at what point auto layout
will set view2
frame so I can set my CALayer
frame accordingly.
As I mentioned in the comments, you don't have to do anything special if your view is aligned with auto layout. If you want a single layer to adjust its bounds to the view's bounds, the most simple and correct way will be overriding
layerClass
method of the view - in that case layer bounds will be adjusted within a view automatically. If you need one more layer in addition to the standard one, the best place to update layer's bounds may besetFrame:
method override.As of
viewDidLayoutSubviews
, it is just an event to inform you, that all subviews of viewcontroller's root view are positioned at the desired places. You are not guaranteed that all the rest subviews of those subviews will also be aligned, since it is a responsibility of the parent view itself. they can be both auto layout positioned and manual layout positioned. This event also does not guarantee that all the the views are displayed as desired, especially if their frame change is animated.