When is layoutSubviews called?

后端 未结 9 1650
忘掉有多难
忘掉有多难 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:21

    calling [self.view setNeedsLayout]; in viewController makes it to call viewDidLayoutSubviews

    0 讨论(0)
  • 2020-11-22 15:24

    Some of the points in BadPirate's answer are only partially true:

    1. For addSubView point

      addSubview causes layoutSubviews to be called on the view being added, the view it’s being added to (target view), and all the subviews of the target.

      It depends on the view's (target view) autoresize mask. If it has autoresize mask ON, layoutSubview will be called on each addSubview. If it has no autoresize mask then layoutSubview will be called only when the view's (target View) frame size changes.

      Example: if you created UIView programmatically (it has no autoresize mask by default), LayoutSubview will be called only when UIView frame changes not on every addSubview.

      It is through this technique that the performance of the application also increases.

    2. For the device rotation point

      Rotating a device only calls layoutSubview on the parent view (the responding viewController's primary view)

      This can be true only when your VC is in the VC hierarchy (root at window.rootViewController), well this is most common case. In iOS 5, if you create a VC, but it is not added into any another VC, then this VC would not get any noticed when device rotate. Therefore its view would not get noticed by calling layoutSubviews.

    0 讨论(0)
  • 2020-11-22 15:26

    I had a similar question, but wasn't satisfied with the answer (or any I could find on the net), so I tried it in practice and here is what I got:

    • init does not cause layoutSubviews to be called (duh)
    • addSubview: causes layoutSubviews to be called on the view being added, the view it’s being added to (target view), and all the subviews of the target
    • view setFrame intelligently calls layoutSubviews on the view having its frame set only if the size parameter of the frame is different
    • scrolling a UIScrollView causes layoutSubviews to be called on the scrollView, and its superview
    • rotating a device only calls layoutSubview on the parent view (the responding viewControllers primary view)
    • Resizing a view will call layoutSubviews on its superview

    My results - http://blog.logichigh.com/2011/03/16/when-does-layoutsubviews-get-called/

    0 讨论(0)
提交回复
热议问题