简体   繁体   中英

Force iOS orientation to landscape on one modal view controller

I have a universal application that supports both iPad and iPhone. On the iPad I support all orientations and on the iPhone, only Portrait.

I want one view controller (which is displayed modally), when running on the iPhone, to be displayed in any orientation that the device is in. I have seen many tutorials and SO posts that suggest using the -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window app delegate method and this seems to work absolutely fine in respect to rotating the modal view controller. However, when I dismiss the modal view whilst in landscape, the entire app remains landscape.

Ideally, as soon as the modal view is dismissed the app should go back into portrait mode.

After investigation, it appears that the -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window callback is not called after dismissing the view controller and I can't figure out why.

I have had a play with the example app here , and the callback is fired when dismissing the view controller, which I can't figure out as to why. The only difference I can see is that my view hierarchy is a lot more complex and that I'm displaying a Navigation Controller rather than an explicit view controller but I don't see how that should affect the callback.

Any ideas as to finding a solution to this?

Thanks

You can try to use this method (working in app store build):

[[UIDevice currentDevice] performSelector:@selector(setOrientation:)
                               withObject:(__bridge id)((void*)UIInterfaceOrientationPortrait)];

In appdelegate:

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

//Hire check your needed device ore other things you need
    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone )
    { 
     //device is iphone
         if ([currentViewController respondsToSelector:@selector(canRotate)]) 
         {
            // Unlock landscape view orientations for this view controller
            return UIInterfaceOrientationMaskAll;
         }
         return UIInterfaceOrientationMaskPortrait;
    }  
    else
    //device Ipad
    return UIInterfaceOrientationMaskAll;  
}

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

in the needed view to rotate make sure to implement

  - (void)canRotate { }

This will allow you to rotate only the view you want for iPhone device.

I got this code with the help of an old stackoverflow post but i can`t find the link to it now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM