IOS 6 revert orientation after dismissModalViewController

iosios6

I have a UITabBarViewController in portrait mode where I push a modalViewController in landscape. The rotation changes to landscape when I push the modal, but when I dismiss it the orientation remains in landscape instead of reverting to portrait.

Code:

a CustomUINavigationController for the landscape mode:

- (BOOL)shouldAutorotate
{
  return [[self.viewControllers lastObject] shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations
{
  return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
  return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

the uitabbarcontroller

- (BOOL)shouldAutorotate
{
  if ([self inModal]) {
    UINavigationController *nav = (UINavigationController *)self.modalViewController;
    return [[nav.viewControllers lastObject] shouldAutorotate];
  } else {
    return NO;
  }
}

-(NSUInteger)supportedInterfaceOrientations
{
  if ([self inModal]) {
    UINavigationController *nav = (UINavigationController *)self.modalViewController;
    return [[nav.viewControllers lastObject] supportedInterfaceOrientations];
  } else {
    return UIInterfaceOrientationPortrait;
  }    
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
  if ([self inModal]) {
    UINavigationController *nav = (UINavigationController *)self.modalViewController;
    return [[nav.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
  } else {
    return UIInterfaceOrientationPortrait;
  }
}

- (BOOL)inModal
{
  if (self.modalViewController && [self.modalViewController isKindOfClass:[UINavigationController class]]) {
    return YES;
  } else {
    return NO;
  }    
}

If I set shouldRotate to YES I get an error: * Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES. Even though I have set the two orientations in UISupportedDeviceOrientations.

Best Answer

I think your problem is caused by this line (you have it twice in your code):

return UIInterfaceOrientationPortrait;

I'm currently trying to do s.th. similar to what you try to achieve, and have asked my question here: iOS 6 auto rotation issue - supportedInterfaceOrientations return value not respected

The problem with your code is that the two methods don't return an interface orientation but rather a mask that encodes in its bits a combination of allowed or preferred orientations.

Try this:

return UIInterfaceOrientationMaskPortrait;

UIInterfaceOrientationPortrait is mapped to 0 as far as I know and is thus interpreted as the empty set of interface orientations (none).