I am considering rotation for iOS 6.0 and above only. Creating category for UINaviagtionController can be a good approach here. So follow these steps
Step 1. Put this code in your AppDelegate.m
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskAll;
}
Step 2. Create a category for UINaviagtionController
Orientation.h
#import <Foundation/Foundation.h>
@interface UINavigationController (Orientation)
@end
Orientation.m
#import "Orientation.h"
@implementation UINavigationController (Orientation)
- (BOOL)shouldAutorotate {
if (self.topViewController != nil)
return [self.topViewController shouldAutorotate];
else
return [super shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations {
if (self.topViewController != nil)
return [self.topViewController supportedInterfaceOrientations];
else
return [super supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
if (self.topViewController != nil)
return [self.topViewController preferredInterfaceOrientationForPresentation];
else
return [super preferredInterfaceOrientationForPresentation];
}
Step 3. Now create a UIViewController class that forces initial orientation. For Portrait
PortraitVC.h
#import <UIKit/UIKit.h>
@interface PortraitVC : ViewController
@end
PortraitVC.m
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
Similarly For Landscape
LandscapeVC.h
#import <UIKit/UIKit.h>
@interface LandscapeVC : ViewController
@end
LandscapeVC.m
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
Step 4. Now your original ViewController which you want to force in Portrait Mode only and without having rotation, write this code in your viewDidLoad
PortraitVC *c = [[PortraitVC alloc] init];
[self presentViewController:c animated:NO completion:NULL];
[self dismissViewControllerAnimated:NO completion:NULL];
And set the orientations like this
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
If you want to force your original ViewController in Landscape Mode only and without having rotation, write this code in your viewDidLoad
LandscapeVC *c = [[LandscapeVC alloc] init];
[self presentViewController:c animated:NO completion:NULL];
[self dismissViewControllerAnimated:NO completion:NULL];
And set the orientations like this
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
I did this because "preferredInterfaceOrientationForPresentation" is called only when we use presentViewController or presentModalViewController. And to set initial orientation "preferredInterfaceOrientationForPresentation" must be called.
Hope this helps.