Search code examples
ios6orientationuiinterfaceorientation

iOs 6 return from UIViewController in landscape to portrait


So I have an application with navigation controller, the last one viewcontoller can be rotated to any orientation. And the others must be in portrait only. So I have troubles when we rotate the last viewController in landscape and then pressing back button on navigation controller. We go to previous one and see that layout are broken. I thought the only way is to use something like this:

[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait] 

(I know it's a hack and it would be preferred to avoid such things) But wherever I tried it - it caused me with different layout problems. The usual methods like:

- (NSUInteger)supportedInterfaceOrientations
- (BOOL)shouldAutorotate

and so on for handle orientation are useless in this situation. Thanks in advance.


Solution

  • I solved this problem for parent of those viewControllers who must be in portrait:

    -(NSUInteger)supportedInterfaceOrientations{
     return UIInterfaceOrientationMaskPortrait;
    }
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
     return UIInterfaceOrientationPortrait;
    }
    - (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
     [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
    }
    

    For rotation viewController:

    -(NSUInteger)supportedInterfaceOrientations{
     return UIInterfaceOrientationMaskAll;
    }
    

    For Navigation Controller:

    - (BOOL)shouldAutorotate {
    
    BOOL result = self.topViewController.shouldAutorotate;
    
     return result;
    }
    
    - (NSUInteger)supportedInterfaceOrientations {
    
    NSUInteger result = self.topViewController.supportedInterfaceOrientations;
    
    return result;
    }
    

    And added this to AppDelegate:

    - (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
    if ([navigationController.topViewController isKindOfClass:[RotationViewController  class]])
        return UIInterfaceOrientationMaskAll;
    else
        return UIInterfaceOrientationMaskPortrait;
    }