问题
Background: I want to make sure my viewControllers rotate properly when it appears. My viewControllers have excellent codes managing the rotation and orientation when it is visible.
Problem: Given two viewControllers in a NavigationController, viewC1
and viewC2
. I did the following:
1.) Set rootViewController to viewC1
2.) Push viewC2
into the NavigationController
3.) Rotate
4.) Pop viewC2
5.) viewC1
is still stucked in the old orientation look (as in the transformation code in willAnimateRotationToInterfaceOrientation
was not called) with the new orientation.
What can I do to ensure viewC1
call willAnimateRotationToInterfaceOrientation
to reconstruct itself to look correctly in the new rotation?
Additional info:
This is all code (no storyboard/xib). I have shouldAutorotateToInterfaceOrientation
return YES
on all the views. I use willAnimateRotationToInterfaceOrientation
to manage all my rotation.
Oh, and please no hacks. For example, copy the code from rotation then check the rotation mannually and manage it in viewDidAppear
.
回答1:
Think about the name of the method, and what you're trying to achieve.
willAnimateRotationToInterfaceOrientation
indicates that the view controlled by the view controller is about to animate to a particular orientation. If your view is in the middle of a navigation stack, then it is not being displayed on screen. To animate something that isn't on screen is costly and ultimately worthless. So, that particular method is out of the question, but the problem that remains is there isn't anything else more appropriate in UIKit. The reason is to rotate something (even if not animated) when it's offscreen is worthless cost. It's the responsibility of the developer to handle a change in orientation when the view appears ("transformation on demand" as you will).
You say you don't want hacks, but the method you've described as a hack is probably the most appropriate thing to do. Having a generic method named something like
-(void) updateLayoutForOrientation:(UIInterfaceOrientation)orientation animated:(BOOL)animated { ... }
isn't a bad idea. This can be the handler for orientation change transformations for the whole view controller.
The places you need to possibly check/handle orientation issues are
-(void) viewWillAppear:(BOOL)animated
-(void) willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation duration: (NSTimeInterval) duration
and in both of these, call updateLayoutForOrientation:animated:
to do the work for you.
来源:https://stackoverflow.com/questions/10837294/ios-orientation-check-rotation-on-viewdidappear