In my app I have multiple views, some views need to support both portrait and landscape, while other views need to support portrait only. Thus, in the project summary, I have all selected all orientations.
The below code worked to disable landscape mode on a given view controller prior to iOS 6:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Since shouldAutorotateToInterfaceOrientation was deprecated in iOS6 I've replaced the above with:
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMask.Portrait;
}
This method is correctly called when the view appears (I can set a breakpoint to ensure this), but the interface still rotates to landscape mode regardless of the fact that I'm returning the mask for portrait modes only. What am I doing wrong?
It seems that it's currently impossible to build an app that has different orientation requirements per view. It seems to only adhere to the orientations specified in the project summary.
The best way for iOS6 specifically is noted in "iOS6 By Tutorials" by the Ray Wenderlich team - http://www.raywenderlich.com/ and is better than subclassing UINavigationController for most cases.
I'm using iOS6 with a storyboard that includes a UINavigationController set as the initial view controller.
//AppDelegate.m - this method is not available pre-iOS6 unfortunately
//MyViewController.m - return whatever orientations you want to support for each UIViewController
In my case I have UINavigationController and my view controller inside. I had to subclass UINavigationController and, in order to support only Portrait, add this method:
So in the UINavigationController subclass I need to check which orientation is supported by the current topViewController.
The answers here pointed me in the correct direction although I couldn't get it to work by just cut and pasting because I am using UINavigationControllers inside of a UITabBarController. So my version in AppDelegate.m looks something like this, which will work for UITabBarControllers, UINavigationControllers or UINavigationControllers within a UITabBarController. If you are using other custom containment controllers, you would need to add them here (which is kind of a bummer).
Another key thing to note is that you must override supportedInterfaceOrientations in your UIViewController subclasses or it will default to what you specified in your Info.plist.
One thing I've found is if you have an old application that is still doing
You will need to update that to:
Once you do this the orientation should begin to work again.
Firstly in order to make your app work in only mode you should be returning
UIInterfaceOrientationMaskLandscape
. In case you want to keep only portrait mode, you are doing things correctly.Just add the
UISupportedInterfaceOrientations
key in the Info.plist and assign the interface orientation values your app intends to keep.Also, you should be returning false from
shouldAutoRotate
in case you want to avoid auto rotation totally. But I would suggest you to return true from here and specify the correct orientations insupportedInterfaceOrientations
method.If your are using a UINavigationController as the root window controller, it will be its
shouldAutorotate
&supportedInterfaceOrientations
which would be called.Idem if you are using a UITabBarController, and so on.
So the thing to do is to subclass your navigation/tabbar controller and override its
shouldAutorotate
&supportedInterfaceOrientations
methods.