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 for
GameViewController
.
- (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: