Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop a modal UIViewController from rotating

The modal UIViewController's parent auto-rotates, but when the modal VC is up I only want it to appear in portrait and not be rotatable. I have tried simply returning NO from shouldAutorotate... in the modal VC, but no joy. Supporting iOS 5+.

Any help greatly appreciated.

like image 638
Happy Pig Avatar asked Dec 21 '22 12:12

Happy Pig


1 Answers

Basically, if you presenting Modal Controller on Any Container Controllers (i.e. UINavigationController) it have autorotation method return YES by default. So you have to make subclass for your UINavigation Controller and Deprecate Autorotation there.

For iOS 5 use method:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

    return (toInterfaceOrientation == UIInterfaceOrientationPortrait) : self.enableRotations;
}

for iOS 6:

- (NSUInteger)supportedInterfaceOrientations {

    switch ([UIDevice currentDevice].userInterfaceIdiom) {

        case UIUserInterfaceIdiomPad:   return UIInterfaceOrientationMaskAll;
        case UIUserInterfaceIdiomPhone: return UIInterfaceOrientationMaskPortrait;
    }
}

- (BOOL)shouldAutorotate {

    return NO;
}
like image 99
Andrey Kozlov Avatar answered Jan 05 '23 11:01

Andrey Kozlov