iOS6: supportedInterfaceOrientations not working (is invoked but the interface still rotates)

前端 未结 15 595
野趣味
野趣味 2020-11-29 04:09

In my app I have multiple views, some views need to support both portrait and landscape, while other views need to support portrait only. Thus, in the project summary, I ha

相关标签:
15条回答
  • 2020-11-29 04:29

    The best way for iOS6 specifically is noted in "iOS6 By Tutorials" by the Ray Wenderlich team - http://www.raywenderlich.com/ and is better than subclassing UINavigationController for most cases.

    I'm using iOS6 with a storyboard that includes a UINavigationController set as the initial view controller.

    //AppDelegate.m - this method is not available pre-iOS6 unfortunately

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;
    
    if(self.window.rootViewController){
        UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presentedViewController supportedInterfaceOrientations];
    }
    
    return orientations;
    }
    

    //MyViewController.m - return whatever orientations you want to support for each UIViewController

    - (NSUInteger)supportedInterfaceOrientations{
        return UIInterfaceOrientationMaskPortrait;
    }
    
    0 讨论(0)
  • 2020-11-29 04:30

    One thing I've found is if you have an old application that is still doing

    [window addSubView:viewcontroller.view];  //This is bad in so may ways but I see it all the time...
    

    You will need to update that to:

    [window setRootViewController:viewcontroller]; //since iOS 4
    

    Once you do this the orientation should begin to work again.

    0 讨论(0)
  • 2020-11-29 04:32

    In my case I have UINavigationController and my view controller inside. I had to subclass UINavigationController and, in order to support only Portrait, add this method:

    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
    }
    

    So in the UINavigationController subclass I need to check which orientation is supported by the current topViewController.

    - (NSUInteger)supportedInterfaceOrientations
    {
        return [[self topViewController] supportedInterfaceOrientations];
    }
    
    0 讨论(0)
提交回复
热议问题