【问题标题】:Only one view controller orientation issue when going back to pervious view controller in ios 7在 ios 7 中返回上一个视图控制器时只有一个视图控制器方向问题
【发布时间】:2014-09-09 14:08:23
【问题描述】:

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

我正在使用下面的代码,它运行良好,但是当我从横向模式返回到可渗透视图控制器时,它不会在纵向模式下旋转。

- (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;
  }
}

【问题讨论】:

    标签: ios7 orientation


    【解决方案1】:

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

    -(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-18
      • 1970-01-01
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多