What is the correct way to propagate shouldAutorotate into a deep modal viewcontroller in iOS6
Consider the following example:
- Create a new sample
Tabbed Applicationin XCode 4.5 - In the
Summary, select all orientations -
Create a new simple
UITabBarController, e.g.MyTabBarViewControllerand add the code- (BOOL)shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAll; } -
In the AppDelegate, replace with
UITabBarControllerbyMyTabBarViewControllerin order to hook the rotationself.tabBarController = [[MyTabBarViewController alloc] init]; -
Now the rotation should work, and in the
FirstViewController, add the code to show a modal viewcontroller on click-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UIViewController * viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil]; [self presentViewController: [[UINavigationController alloc] initWithRootViewController:viewController2] animated:YES completion:nil]; }
Problem:
Now since the SecondViewController is wrapped by a UINavigationController, even I have added shouldAutorotate in SecondViewController and can’t make the upside down rotation done right.
The only fix is to create a custom UINavigationController and also implement shouldAutorotate and this should work.
But this approach sound stupid and it require me to fix all UI class by implementing the shouldAutorotate and I cannot use shorthands such as [UINavigationController alloc] anymore, I must implement all these
initWithRootViewController...UITabBarController and UINavigationController.
Are there any better approach?
Have you tried this:
It’s a notification sent by the device which tell the observer about the rotation. Do not forget to removeObserver when you don’t need anymore