1
votes

In my app we need only one view controller would be in all orientation mode other view controllers will be portrait mode only.

I am using below code and it's working perfectly but when coming back to pervious view controller it's not rotating in portrait mode when I am coming from Landscape mode.

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

1 Answers

1
votes

I've been working against the same issue. The orientation can be forced back to its normal configuration by adding an adjustment in viewWillAppear of the controller you are going back to:

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

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