Handle Orientation Change in iOS6

Till the version less than iOS6, interface orientation change was being handled by shouldAutorotateToInterfaceOrientation method. But in iOS6, this method doesn't work because iOS6 has new callback named "shouldAutorotate". But just by adding this method and returning YES will not work. Some code tweaks is required to get it done. This post helps you to handle all orientation change.

First of all, you need to subclass your Navigation or TabController.

  • Create a new .h and .m files and name it as CustomNavigationController.h and .m respectively.Now add these methods to .m file.
  • - (BOOL)shouldAutorotate{
        return self.topViewController.shouldAutorotate;
    }
    - (NSUInteger)supportedInterfaceOrientations{
        return self.topViewController.supportedInterfaceOrientations;
    }
  • Now create a CustomNavigationControllerobject in AppDelegate class and this in method,  application:DidFinishLaunchingWithOptions: set your newly created navigation controller object as rootViewController for window object. _window.rootViewController = _navController;
  • Now in your ViewController class, if you want your view to rotate, write this.
  • - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    -(NSUInteger)supportedInterfaceOrientations{
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    -(BOOL)shouldAutorotate{
        return YES;
    }
  • If you do not want your ViewController's view to rotate on device orientation, do this.
  • - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    -(BOOL)shouldAutorotate{
        return NO;
    }
    -(NSUInteger)supportedInterfaceOrientations{
        return UIInterfaceOrientationMaskPortrait;
    }