How to make app fully working correctly for autorotation in iOS 6?

16,215

Solution 1

Figured it out.

1) 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;
}

2) if you don't want view controller rotate

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

3) if you want it to be able to rotate

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

BTW , According to your needs ,another related method :

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

Solution 2

If you are using a Tab Bar Controller instead of a Navigation Controller as your root controller, you'll need to similarly subclass UITabBarController.

Also the syntax will be different. I used the following with success. I then used the above examples with success on the view controllers I wanted to override. In my case I wanted the main screen to not rotate but I had a FAQ Screen with Movies that I naturally wanted to enable landscape view. Worked perfectly! Just note the syntax change to self.modalViewController (you'll get a compiler warning if you try to use the syntax for a navigation controller.) Hope this helps!

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)shouldAutorotate
{
    return self.modalViewController.shouldAutorotate;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return self.modalViewController.supportedInterfaceOrientations;
}
Share:
16,215

Related videos on Youtube

Carina
Author by

Carina

git rm $(git ls-files --deleted)

Updated on September 15, 2022

Comments

  • Carina
    Carina over 1 year

    In iOS6, shouldAutorotateToInterfaceOrientation is deprecated. I tried to use supportedInterfaceOrientations and shouldAutorotate to make app working correctly for autorotation but failed.

    this ViewController I don’t want to rotate, but it doesn't work.

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    -(BOOL)shouldAutorotate
    {
        return NO;
    }
    
    -(NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    

    Any ideas? Thanks for any help in advance!

  • Robotic Cat
    Robotic Cat over 11 years
    +1 for answering your own question with some nice code examples
  • voromax
    voromax over 11 years
    Why are you overwriting - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOri‌​entation)interfaceOr‌​ientation? It is deprecated and is never calling
  • Carina
    Carina over 11 years
    It works for iOS 5.1 or later.Unless your app's deployment target is 6.0. @voromax
  • Zigglzworth
    Zigglzworth over 11 years
    Doesn't work. My subclassed UINavigationController never has -(BOOL)shouldAutorotate called.
  • Carina
    Carina over 11 years
    Did you set it as self.window.rootViewController? @Zigglzworth
  • verec
    verec about 11 years
    @voromax "overriding" not "overwriting".
  • marciokoko
    marciokoko over 10 years
    What exactly are we trying to do when subclassing UINavController? I have a tab bar-> navbar -> uitablevc -> uivc. I understand the top hierarchy takes over (tabbarcontroller in my case). I need to force my tablevc portrait & uivc landscape left.