iOS 6 - (BOOL)shouldAutorotate not getting called for navigation controllers pushed viewControllers

13,092

Solution 1

You can check the following link, you need to create custom navigation to support should auto rotate

http://mobileappdevpage.blogspot.in/2012/11/how-to-use-should-autorotateios-6-with.html

The other way you can do this by creating category of UINaviagationController

code for .h file is

@interface UINavigationController (autorotation)

-(BOOL)shouldAutorotate;
-(NSUInteger)supportedInterfaceOrientations;

and code for .m file is

@implementation UINavigationController (autorotation)

-(BOOL)shouldAutorotate
{

    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
    [self.topViewController shouldAutorotate];
    return YES;

}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;

}
@end

Solution 2

I also faced the same issue with the navigation controller. It works fine in the case of all the pushed view controllers, but my scenario was quite different I had one viewcontroller pushed in to the navigation controller (ViewControllerParent) as the root,

NavController
            -- rootViewController (ViewControllerParent)
                                          --- ViewControllerChild1
                                          --- ViewControllerChild2

Due to some project requirement, I was keeping the ViewControllerParent as base, and then I was adding Child viewcontroller's view as subviews to the parent based on user actions. Now I had a scenario where I wanted Child1 not to rotate and Child2 to have rotation.

The problem I faced was that my [self.topViewController] in the Navigation controller class always returns me the Parent Object since I am not pushing the Childs into the nav stack. So my shouldAutorotate methods in my Childs will never be called. So I had to put a class check in the shouldAutorotate method of my parent and then return the rotation value. I did something like this in parent class (ViewControllerParent),it is a kind of workaround but it fixed the issue

-(BOOL)shouldAutorotate
{
  BOOL allowRotation = YES;

   if ([currentlyLoadedChild isKindOfClass:[Child1 class]])
   {
       allowRotation = NO;
   }
   if ([currentlyLoadedChild isKindOfClass:[Child2 class]])
   {
       allowRotation = YES;
   }
   return allowRotation;
} 

-anoop

Share:
13,092
Aditya Deshmane
Author by

Aditya Deshmane

iOS Application Developer

Updated on June 25, 2022

Comments

  • Aditya Deshmane
    Aditya Deshmane almost 2 years

    For my app rootViewController is navgationController.

    I found that pushed controller's

    -(BOOL)shouldAutorotate is not getting called.

    and

    -(NSUInteger)supportedInterfaceOrientations get called only once.

    I have checked correctly in xcode's project summary (or plist) for windows all orientation support.

    I want these method to get called, as there is some uicontrol positioning code which i want to execute programmatically for orientation change.

    I solved this problem by overriding (category) navigation controller's following methods

    -(BOOL)shouldAutorotate;
    
    -(NSUInteger)supportedInterfaceOrientations;
    

    I checked which controller is getting pushed and accordingly called respective pushed controller's uicontrol positioning code in Navigation controller's following method

    (NSUInteger)supportedInterfaceOrientations;
    

    This is working fine but i dont think this is correct way. Please help me out for better solution.

  • S.P.
    S.P. over 11 years
    But mind you supportedInterfaceOrientations will only be called once
  • Mundi
    Mundi over 11 years
    No, sorry. I just tested this on a fresh project. Whenever I turn, both methods get called exactly once.
  • Aditya Deshmane
    Aditya Deshmane over 11 years
    No I'm checking the push controllers supportedInterfaceOrientations the correct way. Everything is working correctly as expected. I just want to know is there any other alternative for solution i found (overriding methods) so that pushed controllers -(BOOL)shouldAutorotate; -(NSUInteger)supportedInterfaceOrientations; will get called automallically whenever orientation changed. So i can skip checking which controller is pushed and calling its UIControl reposition code accordingly from overriden method. As this UIControl repositioning code is already there in controllers orientation methods.
  • Nilesh Kikani
    Nilesh Kikani about 11 years
    It's very very usefull post to me. Thanks yaar