When is layoutSubviews called?

后端 未结 9 1789
忘掉有多难
忘掉有多难 2020-11-22 14:58

I have a custom view that\'s not getting layoutSubview messages during animation.

I have a view that fills the screen. It has a custom subview at the bo

9条回答
  •  盖世英雄少女心
    2020-11-22 15:11

    Building on the previous answer by @BadPirate, I experimented a bit further and came up with some clarifications/corrections. I found that layoutSubviews: will be called on a view if and only if:

    • Its own bounds (not frame) changed.
    • The bounds of one of its direct subviews changed.
    • A subview is added to the view or removed from the view.

    Some relevant details:

    • The bounds are considered changed only if the new value is different, including a different origin. Note specifically that is why layoutSubviews: is called whenever a UIScrollView scrolls, as it performs the scrolling by changing its bounds' origin.
    • Changing the frame will only change the bounds if the size has changed, as this is the only thing propagated to the bounds property.
    • A change in bounds of a view that is not yet in a view hierarchy will result in a call to layoutSubviews: when the view is eventually added to a view hierarchy.
    • And just for completeness: these triggers do not directly call layoutSubviews, but rather call setNeedsLayout, which sets/raises a flag. Each iteration of the run loop, for all views in the view hierarchy, this flag is checked. For each view where the flag is found raised, layoutSubviews: is called on it and the flag is reset. Views higher up the hierarchy will be checked/called first.

提交回复
热议问题