在我的应用程序中,所有视图控制器都有纵向方向,除了一个 . 所以我们把它命名为“ GameViewController ” . GameViewController 仅适用于横向方向 .

这是我在横向模式下显示它的方式:

MyViewController.m

它始终处于纵向方向,并且我们将从中呈现GameViewController .

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

AppDelegate.m

这就是我们如何为GameViewController维护Landscape方向 .

- (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 的横向方向

  • Doesn't matters even if you would change phone to any orientation

  • GameViewController 解雇

  • 将返回纵向 MyViewController

  • 一切都很好看

在以下情况下不按预期工作:

  • 打开应用程序

  • 打开 MyViewController

  • 调用 showGameView 方法

  • Lock Phone

  • 将手机方向更改为横向

  • Unlock phone, come back to the App

  • 您将拥有 MyViewController 的横向方向

  • GameViewController 解雇

  • 一切看起来都很混乱

这是应用程序设置来处理不同的方向:

enter image description here