2

I am trying to programmatically create UI orientation (portrait and landscape) using Objective C for all devices. Here the problem is I have multiple view controllers. I want to use multiple orientation into particular view controller.

For Ex: 

Splash screen (App delegate - Portrait)
Login screen (Portrait)
Home screen (Both)

If I controlled by below method into App delegate root class then I cant enable both orientation into home view controller. Its showing black screen.

- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

    return UIInterfaceOrientationMaskPortrait;
}
Apple_Ajay
  • 227
  • 4
  • 17

1 Answers1

0

You can try by following implementation. What here doing is, we sends the interfaceorientation we need for the visible view controller from app delegate. For that fist finds the visible viewcontroler and gets its supported interface orientation, If supportedInterfaceOrientations method not implemented in the visible view controller, return default orientation.

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  {
    UIViewController *topController = [self topmostViewController];

    if ([topController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
        return [topController supportedInterfaceOrientations];
    }

    return UIInterfaceOrientationMaskAllButUpsideDown;
}

- (UIViewController *)topmostViewController {
    UIViewController *topController = self.window.rootViewController;

    return [self topViewControllerWithRootViewController:topController];
}

- (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.topViewController];// dont use visible view controller, since it will return the presented ViewController, it may be UIAlertController.
    } else if (rootViewController.presentedViewController && ![rootViewController.presentedViewController isKindOfClass:[UIAlertController class]]) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    } else {
        return rootViewController;
    }
}

And if you want to display any view controller with orientation other than UIInterfaceOrientationMaskAllButUpsideDown implement following method in your view controller

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return <whichever orientation you need>;
}

UPDATE:

Make sure that you selected only the orientations you needed for initial view controller in target settings.

Ref: Finding topmost view controller code from this answer with slight modification by addressing UIAlertcontroller presented case

Community
  • 1
  • 1
Johnykutty
  • 12,091
  • 13
  • 59
  • 100