8

I want several of my app viewcontrollers to not rotate in iOS 6.0. This is what i did to make the rotation in iOS 6 possible:

1.) Set the windows rootviewController in application:didFinishLaunchingWithOptions:

self.window.rootViewController = self.tabBarController;

2.) Set the "Supported Interface Orientations" in my Target (in XCode) so i can use all orientations

3.) Implemented the new iOS 6.0 rotation functionality

- (BOOL) shouldAutorotate {

    return YES;
}


-(NSUInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskAll;
}

4.) For some reasons, i subclassed the UINavigationController and implemented also these new functionalities and used this new NavigationController in stead of the original one.

So far so good, everything works fine and all viewcontrollers are now able to rotate to every orientation. Now i want several viewController to not rotate and only stay in portrait. But when i set the new rotations methods in those specific viewcontrollers like this, it still rotates to every orientation:

- (BOOL) shouldAutorotate {

    return NO;
}


-(NSUInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskPortrait;
}

Also setting the navigationController's rotationsfunctionality like above doesn't change anything. (All viewcontrollers can rotate to every orientation)

What am i doing wrong?

EDIT:

Also setting the preferred Interfaceorientation doesn't change anything:

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {

    return UIInterfaceOrientationMaskPortrait;
}
NicTesla
  • 1,626
  • 5
  • 21
  • 35

3 Answers3

11

If you want all of our navigation controllers to respect the top view controller you can use a category. I've found it easier than subclassing.

@implementation UINavigationController (Rotation_IOS6)

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

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

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

@end
Anthony
  • 2,867
  • 1
  • 17
  • 17
  • 5
    Be careful with this solution - if the view controller's supportedInterfaceOrientations method happens to call supportedInterfaceOrientations on the nav controller then you have infinite recursion. I found the hard way that apple's UIPrintingProgressViewController asks the nav controller for supportedInterfaceOrientations... – tyler Nov 20 '12 at 19:45
  • @tyler How do we take care of that? The Print results in infinite recursion. I can't fix it anyhow. I used the above category for rotation of specific view controllers in my app. – Angad Manchanda Oct 24 '13 at 19:19
  • In short, don't use a category, control your rotation in an extension of UINavigationController. Since rotation was so uncommon in our app the simplest thing for me to do was use a UINav category to explicitly lock rotation and then I created a UINav extension that would basically do what the category above does if I needed a nav stack that allowed rotation. – tyler Oct 24 '13 at 21:18
  • @tyler So, I created a subclass of UINavigationController and added the methods to it, now where should I use that subclass? I want to rotate couple of view controllers to landscape in my application and in the plist I have (portrait and landscape enabled) along with PRINT to work. The UIPrintingProgressViewController is resulting in infinite recursion. I am stucked. – Angad Manchanda Oct 24 '13 at 21:54
  • @tyler I got it. Subclassing it worked without any crash or infinite recursions on Print.Thanks – Angad Manchanda Oct 24 '13 at 22:42
0

This works for me:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return NO;
}
Elendas
  • 733
  • 3
  • 8
  • 22
  • 2
    Thanks for your quick answer, but this method is depricated in iOS 6. In iOS 6 you should use the two methods shouldAutorotate and supportedInterfaceOrientations instead. – NicTesla Sep 21 '12 at 08:15
0

You need to create category of UITabBarController to support should auto rotate

code of .h file is as

@interface UITabBarController (autoRotate)<UITabBarControllerDelegate>

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

@end

code of .m file is as

-(BOOL)shouldAutorotate {

    AppDelegate *delegate= (AppDelegate*)[[UIApplication sharedApplication]delegate];
    return [delegate.tabBarController.selectedViewController shouldAutorotate];
}


- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

Note: Name of AppDelegate will be changed with your project's AppDelegate File name.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Shreesh Garg
  • 562
  • 5
  • 18