iOS 6 shouldAutorotate: is NOT being called
I have been scouring the internet for a solution to this but am finding nothing. I am trying to make my iOS 5 app iOS 6 compatible. I cannot get the orientation stuff to work right. I am unable to detect when a rotation is about to happen. Here is the code I am trying:
- (BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
// pre-iOS 6 support
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
The new supportedInterfaceOrientation: method gets called just fine. The shouldAutorotate method, however, will not fire. I need to do some image swapping on rotate, but I can't get any indication that a rotation is about to occur.
Thanks in advance.
Solution 1:
See if you are getting the following error when your App starts.
Application windows are expected to have a root view controller at the end of application launch
If so the way to fix it is by making the following change in the AppDelegate.m
file (although there seem to be a number of answers how to fix this):
// Replace
[self.window addSubview:[navigationController view]]; //OLD
// With
[self.window setRootViewController:navigationController]; //NEW
After this shouldAutoRotate
should be correctly called.
Solution 2:
When using UINavigationController as the basis for an app I use the following subclass to give me the flexibility to allow the top most child viewcontroller to decide about rotation.
@interface RotationAwareNavigationController : UINavigationController
@end
@implementation RotationAwareNavigationController
-(NSUInteger)supportedInterfaceOrientations {
UIViewController *top = self.topViewController;
return top.supportedInterfaceOrientations;
}
-(BOOL)shouldAutorotate {
UIViewController *top = self.topViewController;
return [top shouldAutorotate];
}
@end