返回ios 7中的上一个视图控制器时,只有一个视图控制器方向问题

时间:2014-09-09 14:08:23

标签: ios7 orientation

在我的应用程序中,我们只需要一个视图控制器将处于所有方向模式,其他视图控制器将仅为纵向模式。

我正在使用下面的代码并且它工作得很好但是当回到过去的视觉控制器时,当我从横向模式进入时,它不会以纵向模式旋转。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
  // Get topmost/visible view controller
  UIViewController *currentViewController = [self topViewController];

  // Check whether it implements a dummy methods called canRotate
  if ([currentViewController respondsToSelector:@selector(canRotate)]) {
      // Unlock landscape view orientations for this view controller
      return UIInterfaceOrientationMaskAllButUpsideDown;
  }

  // Only allow portrait (standard behaviour)
  return UIInterfaceOrientationMaskPortrait;
}

- (UIViewController*)topViewController {
  return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
  if ([rootViewController isKindOfClass:[UITabBarController class]]) {
    UITabBarController* tabBarController = (UITabBarController*)rootViewController;
    return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
  } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
    UINavigationController* navigationController = (UINavigationController*)rootViewController;
    return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
  } else if (rootViewController.presentedViewController) {
    UIViewController* presentedViewController = rootViewController.presentedViewController;
    return [self topViewControllerWithRootViewController:presentedViewController];
  } else {
    return rootViewController;
  }
}

1 个答案:

答案 0 :(得分:1)

我一直在努力解决同样的问题。通过在要返回的控制器的viewWillAppear中添加调整,可以强制将方向恢复为正常配置:

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
相关问题