iosuiviewcontrolleruikitboundsviewdidlayoutsubviews

What's exactly viewDidLayoutSubviews?


I was reading the description of viewDidLayoutSubviews of UIViewController:

Called to notify the view controller that its view has just laid out its subviews [...] 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 [...].

For me, it means: "Called when the layout of subviews is complete, but actually this is not true". So what's really behind viewDidLayoutSubviews?


Solution

  • When bounds change for a ViewControllers View, this method is called after the positions and sizes of the subviews have changed.

    1. So this is our chance to make changes to view after it has laid out its subviews, but before it is visible on screen.

    2. Any changes that depending on bounds has to be done, we can do here and not in ViewDidLoad or ViewWillAppear.

    3. While ViewDidLoad & ViewWillAppear, the frame and bounds of a view are not finalised. So when AutoLayout has done it's job of fixing mainView and it's subviews, this method is called.

    When using autolayout, framework does not call layoutSubviews every time. This is called in these cases.

    Note: The call for viewDidLayoutSubviews also depends on various factors like autoresize mask, using Auto-Layout or not, and whether view is in view hierarchy or not.

    For any other clarification, check When is layoutSubviews called?