Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

layoutSubviews called twice when rotating

When my main view rotates I want to re-arrange the sub views, so in my ViewController, I override

willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration

and set the frames of the subViews in there. This is all well and good, but in the subViews I have also overridden

 layoutSubviews

so they layout themselves correctly. But the problem is this now gets called twice - presumably once when I set the Frame in willAnimateRotationToInterfaceOrientation and once because of the rotation. (If I don't set the Frame it gets called once.)

Surely it's the responsibility of the ViewController to layout the frames so this seems like a design flaw - what's the solution so layoutSubviews is only called once?

like image 964
Bbx Avatar asked Jan 29 '13 21:01

Bbx


1 Answers

I had the same question. I found this page to be helpful for me. Is this useful for you?

EDIT: Here is the summary of the page (copied):

  • 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 it’s frame set only if the size parameter of the frame is different
  • scrolling a UIScrollView causes layoutSubviews to be called on the scrollView, and it’s superview
  • rotating a device only calls layoutSubview on the parent view (the responding viewControllers primary view)
  • removeFromSuperview – layoutSubviews is called on superview only (not show in table)
like image 95
Armin Avatar answered Oct 21 '22 04:10

Armin