Ethan Allen
Ethan Allen

Reputation: 14835

How do I make rotation work properly in iOS 6 GM in a tab bar app?

Rotation was working JUST FINE with iOS 5 and now it doesn't work at all. I had it set so that ALL of my views stayed Portrait exception when on Tab 1 when a certain view was open, then users could rotate and it would show a coverflow-style view.

My setup is that I create my tabbar at runtime in the AppDelegate. I then set it as the main root view:

self.window.rootViewController = self.tabBarController;

But ALL of my views, on all tabs, now rotate left or right no matter what. And I've tried adding the new code (from multiple examples in the forums) to no avail.... I breakpoint everything and NO rotation code ever gets called when I rotate my phone.

Each TabController has within it a NavigationController and then within that has my main views with all of my UI.

Any ideas or pointers on how to do rotation correctly in iOS 6? Very frustrating because this is the final problem I need to fix before I can ship.

Upvotes: 3

Views: 1921

Answers (2)

Raj
Raj

Reputation: 1139

I just needed to add the following method in the appDelegate , in order for the rotation to

work on ios 6.

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:

(UIWindow *)window{

           return UIInterfaceOrientationMaskAll;

}

Where as for ios 4 & 5 we still have to use :

(BOOL)shouldAutorotateToInterfaceOrientation:

               (UIInterfaceOrientation)interfaceOrientation in the specific view controller

Upvotes: 0

colinbrash
colinbrash

Reputation: 481

This will get you up and running. Ultimately you really should subclass these UIKit classes instead of using categories, but unfortunately that will not work for third party libraries which are not yet fixed for iOS 6. These categories should work for everything without requiring you to muck about in other people's code.

I have yet to see any solution for the UITabBarController or UINavigationController issues that do not involve subclassing (or writing a category). I wish one existed, though.

Make sure you import the three .h files (or one if you choose to add them all to a single file) at the top of your Prefix.pch file. You must make sure this code is loaded ASAP!

UITabBarController+LegacyRotation.h

#import <UIKit/UIKit.h>

@interface UITabBarController (LegacyRotation)

@end

UITabBarController+LegacyRotation.m

#import "UITabBarController+LegacyRotation.h"

@implementation UITabBarController (LegacyRotation)

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

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

@end

UINavigationController+LegacyRotation.h

#import <UIKit/UIKit.h>

@interface UINavigationController (LegacyRotation)

@end

UINavigationController+LegacyRotation.m

#import "UINavigationController+LegacyRotation.h"

@implementation UINavigationController (LegacyRotation)

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

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

@end

UIViewController+LegacyRotation.h

#import <UIKit/UIKit.h>

@interface UIViewController (LegacyRotation)

@end

UIViewController+LegacyRotation.m

#import "UIViewController+LegacyRotation.h"

@implementation UIViewController (LegacyRotation)

- (NSUInteger)supportedInterfaceOrientations
{
    NSUInteger ret = 0;

    if ([self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait]) {
        ret |= UIInterfaceOrientationMaskPortrait;
    }
    if ([self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortraitUpsideDown]) {
        ret |= UIInterfaceOrientationMaskPortraitUpsideDown;
    }
    if ([self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft]) {
        ret |= UIInterfaceOrientationMaskLandscapeLeft;
    }
    if ([self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeRight]) {
        ret |= UIInterfaceOrientationMaskLandscapeRight;
    }

    return ret;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

@end

Upvotes: 3

Related Questions