How to force a UIViewController to Portrait orientation in iOS 6

后端 未结 16 1336
别那么骄傲
别那么骄傲 2020-11-22 06:03

As the ShouldAutorotateToInterfaceOrientation is deprecated in iOS 6 and I used that to force a particular view to portrait only, what is the c

相关标签:
16条回答
  • 2020-11-22 06:57

    I have a very good approach mixing https://stackoverflow.com/a/13982508/2516436 and https://stackoverflow.com/a/17578272/2516436

    -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
        NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;
    
    
        if(self.window.rootViewController){
            UIViewController *presentedViewController = [self topViewControllerWithRootViewController:self.window.rootViewController];
            orientations = [presentedViewController supportedInterfaceOrientations];
        }
    
        return orientations;
    }
    
    - (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;
        }
    }
    

    and return whatever orientations you want to support for each UIViewController

    - (NSUInteger)supportedInterfaceOrientations{
        return UIInterfaceOrientationMaskPortrait;
    }
    
    0 讨论(0)
  • 2020-11-22 06:57

    Put this in the .m file of each ViewController you don't want to rotate:

    - (NSUInteger)supportedInterfaceOrientations
    {
        //return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
        return UIInterfaceOrientationMaskPortrait;
    }
    

    See here for more information.

    0 讨论(0)
  • 2020-11-22 07:01

    This answer relates to the questions asked in the comments of the OP's post:

    To force a view to appear in a given oriention put the following in viewWillAppear:

    UIApplication* application = [UIApplication sharedApplication];
    if (application.statusBarOrientation != UIInterfaceOrientationPortrait)
    {
        UIViewController *c = [[UIViewController alloc]init];
        [self presentModalViewController:c animated:NO];
        [self dismissModalViewControllerAnimated:NO];
    }
    

    It's a bit of a hack, but this forces the UIViewController to be presented in portrait even if the previous controller was landscape

    UPDATE for iOS7

    The methods above are now deprecated, so for iOS 7 use the following:

    UIApplication* application = [UIApplication sharedApplication];
    if (application.statusBarOrientation != UIInterfaceOrientationPortrait)
    {
         UIViewController *c = [[UIViewController alloc]init];
         [c.view setBackgroundColor:[UIColor redColor]];
         [self.navigationController presentViewController:c animated:NO completion:^{
                [self.navigationController dismissViewControllerAnimated:YES completion:^{
                }];
         }];
    }
    

    Interestingly, at the time of writing, either the present or dismiss must be animated. If neither are, then you will get a white screen. No idea why this makes it work, but it does! The visual effect is different depending on which is animated.

    0 讨论(0)
  • 2020-11-22 07:01

    I have a relatively complex universal app using UISplitViewController and UISegmentedController, and have a few views that must be presented in Landscape using presentViewController. Using the methods suggested above, I was able to get iPhone ios 5 & 6 to work acceptably, but for some reason the iPad simply refused to present as Landscape. Finally, I found a simple solution (implemented after hours of reading and trial and error) that works for both devices and ios 5 & 6.

    Step 1) On the controller, specify the required orientation (more or less as noted above)

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    }
    
    -(BOOL)shouldAutorotate
    {
        return YES;
    }
    
    -(NSUInteger)supportedInterfaceOrientations
    {
        NSInteger mask = UIInterfaceOrientationMaskLandscape;
        return mask;
    
    }
    

    Step 2) Create a simple UINavigationController subclass and implement the following methods

    -(BOOL)shouldAutorotate {
            return YES;
    }
    - (NSUInteger)supportedInterfaceOrientations {
            return UIInterfaceOrientationMaskLandscape;
    }
    

    Step 3) Present your viewController

    vc = [[MyViewController alloc]init];
    MyLandscapeNavigationController *myNavigationController = [[MyLandscapeNavigationController alloc] initWithRootViewController:vc];
    [self myNavigationController animated:YES completion:nil];
    

    Hope this is helpful to someone.

    0 讨论(0)
提交回复
热议问题