Search code examples
xcodeios6landscape-portrait

Portrait and Landscape mode in iOS6


When updating my app to iOS6 standard the portrait / landscape is gone. Ir worked perfectly when I was building with Xcode 3. But now using latest Xcode and latest SDK the rotation is gone and it is always in portrait mode. No matter what I put in "Supported interface Orientations". And the code I used to get rotation before seems to have no effect at all. I had these lines.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    switch (toInterfaceOrientation) {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            return YES;
        default:
            return NO;
    }
}

How do I change and what do I change to get it work again?


Solution

  • First of all, in AppDelegate, write this. THIS IS VERY IMP

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
         return (UIInterfaceOrientationMaskAll);
    }
    

    Then, For UIViewControllers, in which you need only PORTRAIT mode, write these functions

    - (BOOL)shouldAutorotate
    {
         return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
         return (UIInterfaceOrientationMaskPortrait);
    }
    

    For UIViewControllers, which require LANDSCAPE too, change masking to All.

    - (NSUInteger)supportedInterfaceOrientations
    {
        return (UIInterfaceOrientationMaskAllButUpsideDown);
        //OR return (UIInterfaceOrientationMaskAll);
    }
    

    Now, if you want to do some changes when Orientation changes, then use this function.

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
    
    }
    

    EDIT :

    A lot depends on with which controller is your UIViewController embedded in.

    Eg, If its inside UINavigationController, then you might need to subclass that UINavigationController to override orientation methods like this.

    subclassed UINavigationController (the top viewcontroller of the hierarchy will take control of the orientation.) did set it as self.window.rootViewController.

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

    From iOS 6, it is given that UINavigationController won't ask its UIVIewControllers for orientation support. Hence we would need to subclass it.