1

I saw this stackoverflow post:shouldAutorotateToInterfaceOrientation is not working in iOS 6. The answer was to add a category called RotationIn_IOS6. I have done that and the views for the different viewcontrollers are working correctly in iOS6. The problem is in iOS5.

I only need a few views to rotate in all directions, the rest should be in portrait or either PortraitUpsideDown. The problem i face is that either all don't rotate(code1) or after it rotates to landscape, it remains in the same orientation until you rotate back to portrait(code2).

Code1:

@implementation UINavigationController (RotationIn_IOS6)
-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject]  preferredInterfaceOrientationForPresentation];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

        return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
@end

Code 2:

@implementation UINavigationController (RotationIn_IOS6)
-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject]  preferredInterfaceOrientationForPresentation];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if([self.visibleViewController isKindOfClass:[MyClassToRotate class]])
    {
         return [self.visibleViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    }
    else
    {
        return UIInterfaceOrientationIsPortrait(interfaceOrientation);
    }

}

Need some guidance on this. Not sure how to solve this.

EDIT:

Code3:

1) Removed the shouldAutorotateToInterfaceOrientation from the Category. 2) Added this code to my particular class

- (BOOL)shouldAutorotate
{
    //returns true if want to allow orientation change
    return TRUE;


}
- (NSUInteger)supportedInterfaceOrientations
{
    //decide number of origination tob supported by Viewcontroller.
    return UIInterfaceOrientationMaskAll;


}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    //from here you Should try to Preferred orientation for ViewController
    return TRUE;
}

Result -> All the viewcontroller locked in portrait. Cannot access PortraitUpsideDown. Once in my viewcontroller, it can rotate, but when you get out of the viewcontroller, locked in landscape..

EDIT 2:

Each viewcontroller contains these code:

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


- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate {
    return NO;
}
Community
  • 1
  • 1
lakshmen
  • 28,346
  • 66
  • 178
  • 276

3 Answers3

0

No need to override shouldAutorotateToInterfaceOrientation: in the navigation controller category instead for iOS 5 each of your view controller should implement shouldAutorotateToInterfaceOrientation: with the supported orientation. The supported orientations determined by the container controller(navigation controller) is only in the iOS6 and above. In lower version it will call the shouldAutorotateToInterfaceOrientation: of the child view controller which is going to display

Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
0

To solve that problem i've subclassed UINavigationController instead of use a category:

MyNavigationcontroller.h

#import <UIKit/UIKit.h>
@interface MyNavigationcontroller : UINavigationController
@end 

MyNavigationcontroller.m

#import "MyNavigationcontroller.h"
@interface MyNavigationcontroller ()
@end

//Set your init...

-(BOOL)shouldAutorotate {
    if ([[self.viewControllers lastObject] isKindOfClass:NSClassFromString(@"YourClass")]) 
        return YES;
    }
        return NO;
}

-(NSUInteger)supportedInterfaceOrientations {

    if ([[self.viewControllers lastObject] isKindOfClass:NSClassFromString(@"YourClass")]) {
        return UIInterfaceOrientationMaskAll;
    }
        return UIInterfaceOrientationMaskPortraitUpsideDown;
}

@end

Then in each viewcontroller override:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationMaskPortraitUpsideDown);//or your orientation
}

Then, at launch, in the didFinishLaunchingWithOptions: of your app delegate:

NSString *reqSysVer = @"6.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) {
        self.navigationController = [[MyNavigationcontroller alloc] initWithRootViewController:masterViewController];//IOS 6
    }else{
        self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
    }
self.window.rootViewController = self.navigationController;
Mat
  • 7,613
  • 4
  • 40
  • 56
  • @lakesh Strange, i'm actually using that code, are you setting the Supported interface orientations in the Target Summary tab? There you have to activate all the orientation (the 4 grey buttons). – Mat May 31 '13 at 17:04
  • No.. I didn't touch that tab at all..edited the question to show what i add to each of my viewcontroller... – lakshmen May 31 '13 at 17:16
0

The simplest way is Target->General->Deployment Info and tick the orientations you want. E.g. tick only portrait and the app will not auto rotate.

malhal
  • 26,330
  • 7
  • 115
  • 133