在我的应用中,所有视图控制器都有纵向方向,除了一个。让我们把它命名为“GameViewController
”。 GameViewController
仅适用于横向方向。
这是我在横向模式中显示的方式:
MyViewController.m
:
它始终处于纵向方向,并且我们将展示
GameViewController
。
- (void) showGameView {
GameViewController *gameView = (GameViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"GameViewController"];
[self presentViewController:gameView animated:YES completion:nil];
}
AppDelegate.m
这就是我们如何维护
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;
}
当应用转到后台时,我们要求解除
GameViewController
。
- (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
我们将这些设置为
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];
}
在以下情况下按预期工作:
MyViewController
showGameView
方法GameViewController
GameViewController
MyViewController
在以下情况下不按预期工作:
MyViewController
showGameView
方法MyViewController
GameViewController
这是处理不同方向的应用设置: