You need to create a sub class of UIViewController
. And apply interface orientation related changes in this sub class. Extend your view controller in which you want to lock orientation with the sub class. I'll provide an example of this.
I have create class which only shows landscape orientation for view controller.
LandscapeViewController
is a subclass of UIViewController
in which you have to deal with orientations.
LandscapeViewController.h:
#import <UIKit/UIKit.h>
@interface LandscapeViewController : UIViewController
@end
LandscapeViewController.m:
#import "LandscapeViewController.h"
@interface LandscapeViewController ()
@end
@implementation LandscapeViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)shouldAutorotate {
return YES;
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
return YES;
}
else {
return NO;
}
}
@end
Extend your view controller using above subclass.
For example:
#import "LandscapeViewController.h"
@interface SampleViewController : LandscapeViewController
@end