My app can autorotate but I need one of the views to only show in portrait mode and don't know how to achieve this.
I tried this (among other things) but the view in question still rotates:
// ViewController.m
-(BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
Can someone kindly point out what I'm doing wrong? Thanks.
-edit-
It's for iOS 6.1
Swift 3 version the accepted answer:
When a
UINavigationController
is involved, create a category on theUINavigationController
andoverride supportedInterfaceOrientations
.Now, iOS containers (such as UINavigationController) do not consult their children to determine whether they should autorotate.
How to create a category
1. Add a new file (Objective c- category under cocoa touch)
2.
Category
: Orientation on UINavigationController3. Add the above code to
UINavigationController+Orientation.m
As per the documentation.
So we need to override
shouldAutorotate
andsupportedInterfaceOrientation
to targetview controllers
.This will work if you have very simple configuration like your target
view controller
is therootViewController
ofwindow
or being presented covering whole screen.In case when configuration of target view controller is complex like embedded in some other container view controllers.
So may be default implementation of these container view controllers not asking there children for there
supportedInterfaceOrientation
preference.So to allow our target
child view controller
to specify theresupportedIntefaceOrientation
we need to tell there container view controller to do so.You can also check my previous answer here.
and Understanding UIViewController rotation when embed in Container View Controllers.