What's exactly viewDidLayoutSubviews?

后端 未结 2 1221
独厮守ぢ
独厮守ぢ 2020-12-29 14:05

I was reading the description of viewDidLayoutSubviews of UIViewController:

Called to notify the view controller that its vi

2条回答
  •  难免孤独
    2020-12-29 15:02

    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?

提交回复
热议问题