Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent autorotate for one view controller?

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

like image 728
Robert Avatar asked Feb 21 '13 14:02

Robert


People also ask

How do I turn off screen rotation in Swift?

Go it the main and select TARGETS then select Info tab (the plist) and open Supported inferface orientations (iPhone) the click on the ones that you do not need. Just leave Portrait(bottom home button) . That should make the UI stay one way.

How do I turn off landscape mode in iOS?

Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off.

What is a UIViewController?

A UIViewController is an object which manages the view hierarchy of the UIKit application. The UIViewController defines the shared behavior and properties for all types of ViewController that are used in the iOS application. The UIViewController class inherits the UIResponder class.


1 Answers

When a UINavigationController is involved, create a category on the UINavigationController and override supportedInterfaceOrientations.

#import "UINavigationController+Orientation.h"

@implementation UINavigationController (Orientation)

- (NSUInteger)supportedInterfaceOrientations {
   return [self.topViewController supportedInterfaceOrientations];
}

- (BOOL)shouldAutorotate {
    return YES;
}

@end  

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 UINavigationController
3. Add the above code to UINavigationController+Orientation.m

like image 50
Anil Varghese Avatar answered Sep 30 '22 05:09

Anil Varghese