Auto Rotation iOS 8 Navigation Controller

1.2k views Asked by At

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;
}
2

There are 2 answers

1
Misha On BEST ANSWER

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
3
Shivbaba's son On

What if you make Objective C category for UINavigation Controller and Override interface orientation methods...I think u try to control the Autorotation.

Objective C category http://rypress.com/tutorials/objective-c/categories