Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

supportedInterfaceOrientations not called with iOS 7

I searched for an answer to this, but couldn't find anything which solved my problem.

So here's the problem: I have a custom UINavigationController, when creating it the supportedInterfaceOrientations method is called on the rootViewController(only supports portrait). But when pushing an other ViewController onto the stack this method isn't called on the pushed ViewController(supports all but upside-down).

I solved it by calling [self supportedInterfaceOrientations] in the viewDidLoad-method, but i think that's not a good way to solve the problem.

I hope you can help me in that matter.

Here's my code i implemented in the second viewController.

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        [[[UIApplication sharedApplication] delegate] setGlobalOrientationMask:UIInterfaceOrientationMaskAllButUpsideDown];
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    else {
        [[[UIApplication sharedApplication] delegate] setGlobalOrientationMask:UIInterfaceOrientationMaskAll];
        return UIInterfaceOrientationMaskAll;
    }
}

I think the solution from johnMa should work fine for the most apps, but in my case, there's a special problem i think, but i solved it by myself now(not sure if it's a good one, but it works).

I implemented the - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated method on my navigationController-delegate.

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (DEF_SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7")) {
        if ([viewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
            [viewController supportedInterfaceOrientations];
        }
    }
}

I hope this can help others with the same problem.

like image 392
manuelwaldner Avatar asked Dec 05 '22 07:12

manuelwaldner


2 Answers

You should implement these code in your custom NavigationController.

 - (NSUInteger)supportedInterfaceOrientations {
    if ([self.topViewController isMemberOfClass:[RootViewController class]]){
        return UIInterfaceOrientationMaskPortrait;
    }else{
        return UIInterfaceOrientationMaskAllButUpsideDown;
   }  
 }

because when your device rotate, it will ask your app rootController(Custom NavigationController) first, if supportedInterfaceOrientations is not implement there. then it will ask the rootController for supportedInterfaceOrientations.

A view controller that acts as the root view controller of the main window or is presented full screen on the main window can declare what orientations it supports.View Controller Programming Guide

like image 170
johnMa Avatar answered Dec 30 '22 05:12

johnMa


this works fine in iOS 8.

but in iOS 9 (Xcode 7) i became a warning: "Conflicting return type in implementation of 'supportedInterfaceOrientations': 'UIInterfaceOrientationMask' (aka 'enum UIInterfaceOrientationMask') vs 'NSUInteger' (aka 'unsigned long')"

i have changed it to:

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{  
     return UIInterfaceOrientationMaskPortrait;  
}
like image 35
ArNo Avatar answered Dec 30 '22 05:12

ArNo