Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS9 supportedInterfaceOrientationsForWindow stops getting called

Tags:

I want to show 1 or 2 UIViewcontrollers in Landscape mode while others in Portrait. For this purpose I have implemented this function in AppDelegate.

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return self.orientation;
}

where in AppDelegate.h, orientation is:

@property (nonatomic, assign) UIInterfaceOrientationMask orientation;

In UIViewcontroller(s) where I need Landscape orientation. I place this code

-(void)viewWillAppear:(BOOL)animated
{
    self.appDelegate.orientation = UIInterfaceOrientationMaskLandscape;
}

and

-(void)viewWillDisappear:(BOOL)animated
{
    self.appDelegate.orientation = UIInterfaceOrientationMaskPortrait;
}

However, when I go to 'LandscapeViewController' it works ok, I go back, it works ok, I again go to 'LandscapeViewController' its ok, and then when I comes back, supportedInterfaceOrientationsForWindow method stop getting called. Any reason for that? or I am doing something weird?

like image 609
Haris Avatar asked Sep 05 '16 17:09

Haris


1 Answers

If you are using UITabBarController create a custom class for UITabBarController and place this code there

-(UIInterfaceOrientationMask) supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

and place this in your view controllers according to your need.

place this code in appDelegate

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

    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;

    id rootViewController = [self topViewControllerWithRootViewController:window.rootViewController];

    if (rootViewController != nil)
    {
        if ([rootViewController respondsToSelector:@selector(canRotate)])
        {
            return UIInterfaceOrientationMaskLandscape;
        }
    }
    return UIInterfaceOrientationMaskPortrait;
}

-(UIViewController*) topViewControllerWithRootViewController:(id)rootViewController
{

    if (rootViewController == nil)
    {
        return nil;
    }

    if ([rootViewController isKindOfClass:[UITabBarController  class]])
    {
        UITabBarController *selectedTabBarController = rootViewController;
        return [self topViewControllerWithRootViewController:selectedTabBarController.selectedViewController];
    }
    else if ([rootViewController isKindOfClass:[UINavigationController class]])
    {
        UINavigationController *selectedNavController = rootViewController;
        return [self topViewControllerWithRootViewController:selectedNavController.visibleViewController];
    }
    else
    {
        UIViewController *selectedViewController = rootViewController;
        if (selectedViewController.presentedViewController != nil)
        {
            return [self topViewControllerWithRootViewController:selectedViewController.presentedViewController];
        }
    }
    return rootViewController;
}

and place this in your view controller

-(void)canRotate
{

}
like image 133
kashifasif Avatar answered Sep 25 '22 16:09

kashifasif