Shinurag KR Shinurag KR - 1 month ago 22
iOS Question

shouldAutorotate Not called - [Not using storyboard]

My UIViewController not called the method shouldAutorotate. tried several ways for forcefully show the VC in portrait mode.

AppDelegate.m

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.navController = [[UINavigationController alloc] initWithRootViewController:[[VCLogin alloc] init]];
[self.window setRootViewController:self.navController];
[UIApplication sharedApplication].statusBarHidden = YES;
[self.window makeKeyAndVisible];


VCLogin.m

- (BOOL)shouldAutorotate
{
return [self.navigationController.visibleViewController shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationPortraitUpsideDown;
}


And enabled Device Orientation as 'Portrait', 'Landscape Left' and 'Landscape Right'

Answer

I got the solution.

Added a custom UINavigationController class.

navigationControllerViewController.h

#import <UIKit/UIKit.h>

@interface navigationControllerViewController : UINavigationController

@end

and override the following methods

navigationControllerViewController.m

- (BOOL)shouldAutorotate {
    return [self.visibleViewController shouldAutorotate];
}

- (UIInterfaceOrientationMask )supportedInterfaceOrientations {
    return [self.visibleViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [self.visibleViewController preferredInterfaceOrientationForPresentation];
}

then assigned this custom navigation controller inside AppDelegate.m file

// Replace
self.navController = [[UINavigationController alloc] initWithRootViewController:[[VCMasterView alloc] init]];

// With
self.navController = [[navigationControllerViewController alloc] initWithRootViewController:[[VCMasterView alloc] init]];
Comments