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
calling
[self.view setNeedsLayout];
in viewController makes it to call viewDidLayoutSubviews
Some of the points in BadPirate's answer are only partially true:
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.
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.
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 targetsetFrame
intelligently calls layoutSubviews
on
the view having its frame set only
if the size parameter of the frame is
differentlayoutSubviews
to be called on
the scrollView, and its superviewlayoutSubview
on the parent view (the
responding viewControllers primary
view)layoutSubviews
on its superviewMy results - http://blog.logichigh.com/2011/03/16/when-does-layoutsubviews-get-called/