Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 519
MartinMoizard Avatar asked Nov 22 '16 08:11

MartinMoizard


1 Answers

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.

  • Rotating a device: only calls layoutSubview on the parent view (the responding viewControllers primary view)

  • Its own bounds (not frame) changed. (The bounds are considered changed only if the new value is different, including a different origin.)

  • A subview is added to the view or removed from the view.
  • Your application forces layout to occur by calling the setNeedsLayout or layoutIfNeeded method of a view.
  • Scrolling a UIScrollView causes layoutSubviews to be called on the scrollView, and its superview.

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?

like image 146
UdayM Avatar answered Sep 25 '22 13:09

UdayM