Maintain one view controller in Portrait and other in Landscape mode iOS

Viewed 1980

I have an iOS app with 2 view controllers namely - FirstViewController and SecondViewController. My window's rootViewController is UINavigationController.

FirstViewController is supposed to work ONLY in portrait mode and SecondViewController ONLY in Landscape mode.

Searching all over Stackoverflow I found out that for iOS6 and above I have to create a category over UINavigationController and override -supportedInterfaceOrientations

THE PROBLEM

Starting from FirstViewController. Now my phone being in Portrait mode, I push SecondViewController, the view loads in portrait mode. Once I rotate my phone to be in landscape the view will rotate to landscape ( and from this point onwards will not return to portrait at all ).

When I pop back FirstViewController will be in Portrait again ( no matter what the orientation of the phone ).

I want that SecondViewController shouldn't be displayed in Portrait mode at all. I've racked my brains all day...cannot find a solution.

APPDELEGATE

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    ViewController *vc = [[ViewController alloc] init];

    self.navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
    [self.navigationController setNavigationBarHidden:YES];
    self.window.rootViewController = self.navigationController;

    [self.window makeKeyAndVisible];

    return YES;
}

FirstViewController

- (IBAction)btnClicked:(id)sender
{
    SecondViewController *vc = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:vc animated:NO];
}

#pragma mark - Rotation handlers
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

SecondViewController

- (IBAction)btnClicked:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark - Rotation handlers
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;
}

UINavigation Category

@implementation UINavigationController (Rotation)

-(BOOL)shouldAutorotate
{
    //return [self.topViewController shouldAutorotate];
    return YES;
}


-(NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}
@end
2 Answers

in my previous app i have done it

first you need to enable Portrait , Landscape left , Landscape right Orientation to project

Now

set below code to your FirstViewController

-(void) viewWillAppear:(BOOL)animated{
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]; 
 }
      - (BOOL)shouldAutorotate
 {
       return NO;
 }

set below code to your secondViewController

#define degreesToRadian(x) (M_PI * (x) / 180.0)


 @implementation UINavigationController (Rotation_IOS6)

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

 -(NSUInteger)supportedInterfaceOrientations
 {
   return [[self.viewControllers lastObject] supportedInterfaceOrientations];
 }

 @end
 @interface secondViewController ()

 @end

 @implementation secondViewController

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
    self.view.transform = CGAffineTransformIdentity;
    self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));

 }
 - (BOOL)shouldAutorotate
{
   return YES;

 }

 - (NSUInteger)supportedInterfaceOrientations
 {
    return UIInterfaceOrientationMaskLandscape;
 }


  @end
Related