1

In my app, all view controllers are having Portrait orientation except one. So let's name it, "GameViewController". The GameViewController is sticked to Landscape orientation only.

This is how I'm showing it in landscape mode:

MyViewController.m:

It's always in Portrait orientation, and from where we'll be presenting GameViewController.

- (void) showGameView {
    GameViewController *gameView = (GameViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"GameViewController"];
    [self presentViewController:gameView animated:YES completion:nil];
}

AppDelegate.m

This is how we're maintaining Landscape orientation forGameViewController.

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if ([self.window.rootViewController.presentedViewController isKindOfClass: [GameViewController class]]) {
        GameViewController *gameView = (GameViewController *) self.window.rootViewController.presentedViewController;
        if (gameView.isPresented) {
            return UIInterfaceOrientationMaskLandscape;
        } else {
            return UIInterfaceOrientationMaskPortrait;
        }
    }
    else return UIInterfaceOrientationMaskPortrait;
}

We've requirement to dismiss GameViewController when app goes to background.

- (void)applicationDidEnterBackground:(UIApplication *)application {
    if(self.window.rootViewController.presentedViewController) {
        UIViewController *presentedVC = self.window.rootViewController.presentedViewController;
        if([presentedVC isKindOfClass:[GameViewController class]]) {
            GameViewController *gameView = (GameViewController *)self.window.rootViewController.presentedViewController;
            gameView.isPresented = NO;
            [gameView.presentingViewController dismissViewControllerAnimated:NO completion:nil];
        }
    }
}

GameViewController.m

We're setting these to make it available in AppDelegate.

- (void) awakeFromNib {
    self.isPresented = YES;
}

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.isPresented = YES;
}

- (void) dismissView {
      self.isPresented = NO;
      [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

Working as per expectation in following cases:

  • Open App
  • Open MyViewController
  • Call showGameView method
  • You will have a landscape orientation for GameViewController
  • Doesn't matters even if you would change phone to any orientation
  • Dismiss from GameViewController
  • Will come back to Portrait orientation for MyViewController
  • Everything looks good

Not working as per expectation in following cases:

  • Open App
  • Open MyViewController
  • Call showGameView method
  • Lock Phone
  • Change phone orientation to Landscape
  • Unlock phone, come back to the App
  • You will have a landscape orientation for MyViewController
  • Dismiss from GameViewController
  • Everything looks messed

This is app settings to handle different orientations:

enter image description here

Hemang
  • 26,840
  • 19
  • 119
  • 186
  • Did you try to allow just the Portrait option in the app settings? – Luca Iaco Mar 31 '16 at 10:20
  • 1
    Yes, in that case, it will not rotate to landscape, I forgot, it was thrown an exception as well. @LucaIaco – Hemang Mar 31 '16 at 10:23
  • Mmm, and what if you generate orientation notification with `[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]` just before presenting the GameViewController and then stopping it on dismiss with `[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]` ? Would it works? – Luca Iaco Mar 31 '16 at 10:27
  • 1
    I haven't tried that, let me try this. Thanks. – Hemang Mar 31 '16 at 10:39
  • 1
    @LucaIaco, it doesn't help. – Hemang Mar 31 '16 at 10:53
  • check this. it might help : http://stackoverflow.com/a/27561781/4569257 – Omar Chaabouni Mar 29 '17 at 19:46

0 Answers0