I have certain viewControllers
which are managed by a UINavigationController
(push and pop). I want to restrict different viewControllers
to different orientations
like the first one should be only in Portrait
, second in portrait
, third in landscape
and fourth can be portrait
and landscape
both. I set a ViewController
on isInitialViewController
from storyBoard
, the
- (BOOL) shouldAutorotate{
return NO;
}
worked without any problem but when i set the navigation controller
(managing these four views by push and pop) as isInitialViewController
from storyBoard
, this function stopped being called and now autoratates
. How can I stop autorotating
these views using this UINavigationController
as the isInitialViewController
. I use the following functions depends on which ViewController
it is
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return (interfaceOrientation == UIDeviceOrientationPortrait);//choose portrait or landscape}
- (BOOL) shouldAutorotate{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
//return UIInterfaceOrientationMaskLandscape;
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
// return UIInterfaceOrientationLandscapeLeft |
// UIInterfaceOrientationLandscapeRight;
return UIInterfaceOrientationPortrait;
}
Just subclass UINavigationController and override appropriate methods:
.h File:
@interface CustomUINavigationController : UINavigationController
@property BOOL canRotate;
@end
.m File:
@implementation CustomUINavigationController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotate
{
return self.canRotate;
}
@end