Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable autorotate on a single UIViewController in iOS6

I have a project using UINavigationController and segues working properly good, all of them rotate correctly, the thing is... I just want to disable autorotation on a specific UIViewController. I tried this:

- (BOOL)shouldAutorotateToInterfaceOrientation:
                               (UIInterfaceOrientation)interfaceOrientation {    
    return NO;
}

// New Autorotation support for iOS 6.
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0){
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

but it's not working, my UIViewController keeps rotating automatically, any help will be welcome :)

like image 268
Jesús Ayala Avatar asked Jun 28 '13 17:06

Jesús Ayala


2 Answers

Per the View Controller Programing Guide

If you want to temporarily disable automatic rotation, avoid manipulating the orientation masks to do this. Instead, override the shouldAutorotate method on the initial view controller. This method is called before performing any autorotation. If it returns NO, then the rotation is suppressed.

So you need to subclass 'UINavigationController', implement shouldAutorotate and use your navigation controller class in your storyboard.

- (BOOL)shouldAutorotate
{
    id currentViewController = self.topViewController;

    if ([currentViewController isKindOfClass:[DetailViewController class]])
        return NO;

    return YES;
}
like image 96
GayleDDS Avatar answered Oct 18 '22 00:10

GayleDDS


Gonna complete GayleDDS's answer for the newbies just added a subclass of UINavigationController as he suggested like this:

#import "UINavigationController.h"
#import "MonthCalendarVC.h"

@implementation UINavigationController (overrides)
- (BOOL)shouldAutorotate
{
    id currentViewController = self.topViewController;

    if ([currentViewController isKindOfClass:[MonthCalendarVC class]])
        return NO;

    return YES;
}
@end

MonthCalendarVC is the viewController I want to be just in portrait mode (fixed), then just added the import to my appdelegate.m

#import "UINavigationController.h"

and that's it

like image 26
Jesús Ayala Avatar answered Oct 18 '22 02:10

Jesús Ayala