Search code examples
iosobjective-corientationlandscape-portraitportrait

iOS (9 &10) App does not rotate upside down


I have troubles, forcing my app to rotate upside down if I am holding the phone upside down.

I have a storyboard file with two scenes: NavigationControllerScenes and MainScene / SettingsScene

In the info.plist I checked the boxes to start in portrait or upside down orientation.

In the app delegate I added:

#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    #else
    - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    #endif
    {
      return UIInterfaceOrientationMaskPortrait;
    }

In Settings view controller:

        - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation // iOS 6 autorotation fix
    {
        return UIInterfaceOrientationPortraitUpsideDown;
    }


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

    - (BOOL)shouldAutorotate  // iOS 6 autorotation fix
    {
        return YES;
    }
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }

#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
        - (NSUInteger)supportedInterfaceOrientations{
    #else
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations
    #endif
    {
        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
    }

}

In Mainview Controller

        - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation // iOS 6 autorotation fix
    {
        return UIInterfaceOrientationPortraitUpsideDown;
    }


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

    - (BOOL)shouldAutorotate  // iOS 6 autorotation fix
    {
        return YES;
    }
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }


    #if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
            - (NSUInteger)supportedInterfaceOrientations
    #else
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations
    #endif
    {
        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
    }

It is possible to rotate to landscape and portrait, but not to portrait upside down. If I uncheck the box of portrait in the info.plist and have only the upside down box checked, the app will crash right in the beginning. So I think a settings is missing...

What I dont get is, I can successfully use those code snippets in a different app with only one scene - so I think the error comes from multiple scenes. Maybe I need to create a navigationController which overrides some methods.

One more thing: This does not work - the app executes the statements but nothing happens:

NSNumber * value = [NSNumber numberWithInt:UIInterfaceOrientationPortraitUpsideDown];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];

In the app with one scene it works!


Solution

  • Finally I found the answer myself:

    Create a CustomNavigationController class as follows:

    // .h file
    #import <UIKit/UIKit.h>
    @interface CustomNavigationController : UINavigationController
    @end
    
    // .m file
    #import "CustomNavigationController.h"
    
    @interface CustomNavigationController ()
    
    @end
    
    @implementation CustomNavigationController
    
    - (BOOL)shouldAutorotate
    {
        return self.topViewController.shouldAutorotate;
    }
    - (NSUInteger)supportedInterfaceOrientations
    {
        return self.topViewController.supportedInterfaceOrientations;
    }
    
    @end
    

    In the storyboard click on the navigation controller scene. Add the created class in the custom class section as shown here