4

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.

Andriy
  • 2,767
  • 2
  • 21
  • 29
Happy Pig
  • 117
  • 3
  • 9

3 Answers3

11

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;
}
Andrey Kozlov
  • 221
  • 1
  • 2
  • Thanks Andrey. Worked a charm. – Happy Pig Dec 14 '12 at 13:35
  • Hi Andrey, what you mean by subclass for your container? do you mean there is need to use a subclass of the actual container ? mean move all the code from the actual controller to a subclass of it ? Thanx. – Malloc Sep 10 '13 at 13:34
0

It can be doable, but I would think twice before doing that.

As a user;

  • I launch the app and use in landscape.
  • trigger an event (eg. press button).
  • see a view which is portrait.
  • rotate the phone and look in portrait.
  • close the modal view.
  • get landscape view again.
  • need to rotate the phone again

Bad usability...

Mert
  • 6,025
  • 3
  • 21
  • 33
0

you have to present that new controller on a new navigation controller.This will solve your problem

Vishal Singh
  • 4,400
  • 4
  • 27
  • 43