How to force a UIViewController to Portrait orientation in iOS 6

后端 未结 16 1337
别那么骄傲
别那么骄傲 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:43

    I did not test it myself, but the documentation states that you can now override those methods: supportedInterfaceOrientations and preferredInterfaceOrientationForPresentation.

    You can probably achieve what you want y setting only the orientation that you want in those methods.

    0 讨论(0)
  • 2020-11-22 06:43

    The answers using subclasses or categories to allow VCs within UINavigationController and UITabBarController classes work well. Launching a portrait-only modal from a landscape tab bar controller failed. If you need to do this, then use the trick of displaying and hiding a non-animated modal view, but do it in the viewDidAppear method. It didn't work for me in viewDidLoad or viewWillAppear.

    Apart from that, the solutions above work fine.

    0 讨论(0)
  • 2020-11-22 06:43

    1) Check your project settings and info.plist and make sure that only the orientations you want are selected.

    2) add the following methods to your topmost view controller(navigation controller/tabbar controller)

    - (NSUInteger) supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    
    }
    

    3) add the following methods to your app delegate

    - (NSUInteger) supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    
    }
    
    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        return UIInterfaceOrientationMaskPortrait;
    
    }
    
    0 讨论(0)
  • 2020-11-22 06:44

    So I ran into the same problem when displaying portrait only modal views. Normally, I'd create a UINavigationController, set the viewController as the rootViewController, then display the UINavigationController as a modal view. But with iOS 6, the viewController will now ask the navigationController for its supported interface orientations (which, by default, is now all for iPad and everything but upside down for iPhone).

    Solution: I had to subclass UINavigationController and override the autorotation methods. Kind of lame.

    - (BOOL)shouldAutorotate {
        return NO;
    }
    
    - (NSUInteger)supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskPortrait;
    }
    // pre-iOS 6 support 
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
        return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    0 讨论(0)
  • 2020-11-22 06:44

    IOS 5

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    
    }
    

    IOS 6

    -(BOOL)shouldAutorotate{
        return YES;
    }
    
    -(NSInteger)supportedInterfaceOrientations{
    
        //    UIInterfaceOrientationMaskLandscape;
        //    24
        //
        //    UIInterfaceOrientationMaskLandscapeLeft;
        //    16
        //
        //    UIInterfaceOrientationMaskLandscapeRight;
        //    8
        //
        //    UIInterfaceOrientationMaskPortrait;
        //    2
    
        //    return UIInterfaceOrientationMaskPortrait; 
        //    or
              return 2;
    }
    
    0 讨论(0)
  • 2020-11-22 06:53

    Not to be dull here, but would you be so kind to share your subclass? Thank you.

    edit: well, I finally did it, the subclass was dead simple to do. I just had to declare the navigationController in the AppDelegate as UINavigationControllerSubclass instead of the default UINavigationController, then modified your subclass with:

    - (BOOL)shouldAutorotate {
        return _shouldRotate;
    }
    

    so I can set any view I want to rotate or not by calling at viewDidLoad

    _navController = (UINavigationController *)self.navigationController;
    [_navController setShouldRotate : YES / NO]
    

    Hope this tweak will help others as well, thanks for your tip!

    Tip: Make use of

    - (NSUInteger)supportedInterfaceOrientations
    

    in your view controllers, so you don't end up by having a portrait desired view in landscape or vice versa.

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