I have UiNavigationController in my app. I want that only one screen will be able to rotate so i put in this class :
-(BOOL)shouldAutorotateToInterfaceOrient
Use the below code in the class you want to autorotate only:
@interface UITabBarController (rotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
- (NSUInteger)supportedInterfaceOrientations;
- (BOOL)shouldAutorotate;
@end
@implementation UITabBarController (rotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if ([self.selectedViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *navController = (UINavigationController *) self.selectedViewController;
if ([[navController visibleViewController] isKindOfClass:[CLASS_NAME_FOR_ROTATION class]])
return YES;
}
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSUInteger)supportedInterfaceOrientations
{
if ([self.selectedViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *navController = (UINavigationController *) self.selectedViewController;
if ([[navController visibleViewController] isKindOfClass:[CLASS_NAME_FOR_ROTATION class]])
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate
{
if ([self.selectedViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *navController = (UINavigationController *) self.selectedViewController;
if ([[navController visibleViewController] isKindOfClass:[CLASS_NAME_FOR_ROTATION class]])
return YES;
}
return NO;
}
@end
Use this code in the parent View Controller of the class (i.e. the just previous class on the stack of navigation controller) that is to be rotated.
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
In your appDelegate add the this code
@property(nonatomic,assign)BOOL shouldRotate;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
shouldRotate=NO;
}
-(void)shouldAutoRotate:(BOOL)rotate
{
self.shouldRotate=rotate;
}
And in your rootview controller add this code
#import "AppDelegate.h"
#define myAppDelegate (AppDelegate *)[[UIApplication sharedApplication] delegate]
- (NSUInteger)supportedInterfaceOrientations
{
if(![myAppDelegate shouldRotate])
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskAllButUpsideDown;
}
After that add this code in viewcontroller.m which is you want to rotate
- (void)viewDidLoad
{
[super viewDidLoad];
[myAppDelegate shouldAutoRotate:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
[myAppDelegate shouldAutoRotate:NO];
}
I have done this for one of the my projects( IOS 7).It works for me perfectly.
You can create a category to override the methods in navigationController to support all classes.
@implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end
If you want to restrict the rotation in different controllers then override the supportedInterfaceOrientations and shouldAutorote in respective viewcontrollers changing the return value as required.
-(NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
For iOS 6, I am using the following code in my app, which allows you to specify rotation for each viewcontroller individually:
AppDelegate.m -
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{NSUInteger orientations =UIInterfaceOrientationMaskAllButUpsideDown;
if(self.window.rootViewController){
UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
orientations = [presentedViewController supportedInterfaceOrientations];
}
return orientations;
}
ViewController.m -
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
Credits for the code originially I believe go to the Ray Wenderlich "iOS 6 by Tutorials" book. Ray Wenderlich website