disable autorotate on a single UIViewController in iOS6
Take a look at this other approach :
http://www.sebastianborggrewe.de/only-make-one-single-view-controller-rotate/
You just have to implement canRotate in a ViewController to allow rotation.
Works fine on iOS 7.
2015-01-30 Because sebastian's site seems to not work (404 error), this is my interpretation of its solution :
Unlike sebastian, I prefer using a protocol (like interface in C#) to avoid to create a method "-(void)canrotate:" in each of my view controller.
IRotationCapabilities.h
-----------------------
#ifndef NICE_APPS_IRotationCapabilities_h
#define NICE_APPS_IRotationCapabilities_h
@protocol IRotationCapabilities < NSObject >
// Empty protocol
@end
#endif
FirstViewController.h
---------------------
- ( void )viewWillAppear:( BOOL )animated
{
[ super viewWillAppear:animated ];
// Forces the portrait orientation, if needed
if( ![ self conformsToProtocol:@protocol( IRotationCapabilities ) ] )
{
if( self.navigationController.interfaceOrientation != UIInterfaceOrientationPortrait )
{
[ [ UIDevice currentDevice ] setValue:@( 1 ) forKey:@"orientation" ];
}
}
}
SecondViewController.h
-----------------------
#import "IRotationCapabilities.h"
@interface SecondViewController : UIViewController < IRotationCapabilities >
AppDelegate.m
-------------
#pragma mark - Orientation management
- ( NSUInteger )application:( UIApplication * )application supportedInterfaceOrientationsForWindow:( UIWindow * )window
{
if( __iPhone )
{
// Gets topmost/visible view controller
UIViewController * currentViewController = [ self topViewController ];
// Checks whether it implements rotation
if( [ currentViewController conformsToProtocol:@protocol( IRotationCapabilities ) ] )
{
// Unlock landscape view orientations for this view controller
return ( UIInterfaceOrientationMaskAllButUpsideDown );
}
// Allows only portrait orientation (standard behavior)
return ( UIInterfaceOrientationMaskPortrait );
}
else
{
// Unlock landscape view orientations for iPad
return ( UIInterfaceOrientationMaskAll );
}
}
Try to implement that in your UIViewController:
// implements the interface orientation (iOS 6.x)
@interface UINavigationController (RotationNone)
-(NSUInteger)supportedInterfaceOrientations;
@end
@implementation UINavigationController (RotationNone)
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
@end
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;
}
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