iOS: Rotating an iPad, what stage to change view?
-
28-04-2021 - |
Question
My app has very different UI structures for each orientation.
At what stage in the rotation lifecycle of an iPad should I remove, then add the orientation's UI to ensure the smoothest transition?
Solution
When handling autorotation, you have 4 main methods to care about:
shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
Now, the way I handle the rotation is, e.g.:
adjust all the frames in
willAnimateRotationToInterfaceOrientation
;add/remove subviews (if needed) in
willRotateToInterfaceOrientation
;restore subviews (if needed) in
didRotateFromInterfaceOrientation
.
More in general, I modify all properties that are animatable in willAnimateRotationToInterfaceOrientation
; while I do all modifications that are not animatable in willRotateToInterfaceOrientation
/didRotateFromInterfaceOrientation
.
This is what the UIViewController reference says about willRotate
/didRotate
:
To temporarily turn off features that are not needed or might otherwise cause problems during the orientation change, you can override the willRotateToInterfaceOrientation:duration: method and perform the needed actions there. You can then override the didRotateFromInterfaceOrientation: method and use it to reenable those features once the orientation change is complete.