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

前端 未结 15 596
野趣味
野趣味 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:21

    Try add shouldAutorotate method

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

    The answers here pointed me in the correct direction although I couldn't get it to work by just cut and pasting because I am using UINavigationControllers inside of a UITabBarController. So my version in AppDelegate.m looks something like this, which will work for UITabBarControllers, UINavigationControllers or UINavigationControllers within a UITabBarController. If you are using other custom containment controllers, you would need to add them here (which is kind of a bummer).

    - (UIViewController*)terminalViewController:(UIViewController*)viewController
    {
      if ([viewController isKindOfClass:[UITabBarController class]])
      {
        viewController = [(UITabBarController*)viewController selectedViewController];
        viewController = [self terminalViewController:viewController];
      }
      else if ([viewController isKindOfClass:[UINavigationController class]])
      {
        viewController = [[(UINavigationController*)viewController viewControllers] lastObject];
      }
    
      return viewController;
    }
    
    - (NSUInteger)application:(UIApplication *)application
    supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
      NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
      UIViewController* viewController = [self terminalViewController:window.rootViewController];
    
      if (viewController)
        orientations = [viewController supportedInterfaceOrientations];
    
      return orientations;
    }
    

    Another key thing to note is that you must override supportedInterfaceOrientations in your UIViewController subclasses or it will default to what you specified in your Info.plist.

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

    try change this code in AppDelegate.m

    //   self.window.rootViewController = self.navigationController;
    
        [window setRootViewController:navigationController];
    

    this is the complete answer

    shouldAutorotateToInterfaceOrientation not being called in iOS 6

    XD

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

    Firstly in order to make your app work in only mode you should be returning UIInterfaceOrientationMaskLandscape. In case you want to keep only portrait mode, you are doing things correctly.

    Just add the UISupportedInterfaceOrientations key in the Info.plist and assign the interface orientation values your app intends to keep.

    Also, you should be returning false from shouldAutoRotate in case you want to avoid auto rotation totally. But I would suggest you to return true from here and specify the correct orientations in supportedInterfaceOrientations method.

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

    If your are using a UINavigationController as the root window controller, it will be its shouldAutorotate & supportedInterfaceOrientations which would be called.

    Idem if you are using a UITabBarController, and so on.

    So the thing to do is to subclass your navigation/tabbar controller and override its shouldAutorotate & supportedInterfaceOrientations methods.

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

    Basically as someone stated above, but in more detail:

    1. Create a new file that is a subclass of UINavigationController
    2. Go to your storyboard and then click on the Navigation Controller, set its class to the one that you just created
    3. In this class(.m file) add the following code so it will remain in portrait mode:

      (BOOL)shouldAutorotate
      {
        return NO;
      } 
      
      (NSUInteger)supportedInterfaceOrientations
      {
       return UIInterfaceOrientationMaskPortrait;
      }
      

    This worked for me

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