I have an iPhone app I am updating to iOS 6 that is having rotation issues. I have a UITabBarController
with 16 UINavigationCotrollers
. Most of the subviews can work in portrait or landscape but some of them are portrait only. With iOS 6 things are rotating when they shouldn't.
I tried subclassing the tabBarController to return the supportedInterfaceOrienations
of the current navigationController's selected viewController:
- (NSUInteger)supportedInterfaceOrientations{
UINavigationController *navController = (UINavigationController *)self.selectedViewController;
return [navController.visibleViewController supportedInterfaceOrientations];
}
This got me closer. The view controller won't rotate out of position when visible, but if I am in landscape and switch tabs the new tab will be in landscape even if it isn't supported.
Ideally the app will only be in the supported orienation of the current visible view controller. Any ideas?
I think is better something like that (as a category method)
this ensures that the method is implemented. If you aren't doing this check and the method is not implemented (like in iOS5 env) the app should crash!
Subclass your
UITabBarController
overriding these methods:Subclass your
UINavigationController
overriding these methods:Then implement these methods in your viewControllers that you do not want to rotate:
And for viewControllers that you do want to rotate:
Your tabbarController should be added as the RootviewController of the app window. If you plan to support the default orientations, all but upsidedown is default for iPhone, then you do not need to do anything else. If you want to support upside-down or if you do not want to support another of the orientations, then you need to set the appropriate values in app delegate and/or info.plist.
I had issue that some View controllers in the navigation stack support all the orientations, some only portrait, but
UINavigationController
was returning all app supported orientations, this little hack helped me. I'm not sure if this is intended behavior or whatIf you plan to enable or disable rotation for all view controllers you don't need to subclass
UINavigationController
. Instead use:in your
AppDelegate
.If you plan to support all orientations in your app but different orientations on PARENT View Controllers (
UINavigationController
stack for example) you should usefrom
AppDelegate
in combination with the following methods in your PARENT View Controller.and
But if you plan to have different orientation settings in different CHILDREN ViewControllers in the same navigation stack (like me) you need to check the current ViewController in the navigation stack.
I've created the following in my
UINavigationController
subclass:Since you cannot control anymore from children ViewControllers the rotation settings of presented view controller you must somehow intercept what view controller is currently in the navigation stack. So that's what I did :). Hope that helps !