Archive

Posts Tagged ‘uitabbarcontroller’

Change iPhone/iPod app orientation within a UITabBarController

September 4th, 2008 12 comments

The documentation for the official iPhone SDK is less than stellar, and as a result I’ve encountered lots of frustrating little problems. In particular, I wasn’t able to get my app to change its orientation when the phone was rotated. UIViewController has a method shouldAutorotateToInterfaceOrientation: that gets called when the iPhone/iPod orientation is changed by the user, and overriding this method to return YES should cause your view to be rotated. You may have implemented this method and to your frustration, nothing happened when you rotated the device. The trick is that Cocoa only calls shouldAutorotateToInterfaceOrientation: on the top most UIViewController. Meaning if your program lives inside a UITabBarController (possibly with a UINavigationController inside some of the tab bar items), the UIViewController that the user is currently interacting with, is not necessarily the one receiving the method call.

In my app, everything lives inside a UITabBarController, so that’s what was receiving the shouldAutorotateToInterfaceOrientation: call. In order to get orientation changes to work you need to subclass UITabBarController (contrary to Apple doc recommendations) and override the method so you can return YES when it’s called. Below is some code for a simple UITabBarController subclass used inside a program that supports a horizontal view.

RotatingTabBarAppDelegate.h

#import <UIKit/UIKit.h>

#import "RotatingTabBarController.h"

/*
 Nothing special implemented in our delegate for this example.
 */

@class RotatingTabBarAppViewController;

@interface RotatingTabBarAppAppDelegate : NSObject <UIApplicationDelegate> {
    IBOutlet UIWindow *window;
}

@property (nonatomic, retain) UIWindow *window;

@end

RotatingTabBarAppDelegate.m

#import "RotatingTabBarAppAppDelegate.h"

@implementation RotatingTabBarAppAppDelegate

@synthesize window;

/*
 We’ll programmatically lay out a simple GUI for this demonstration.
 */

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    
    // Just make two empty view controllers for fun
    UIViewController *tab1 = [[UIViewController alloc] init];
    tab1.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:0];

    UIViewController *tab2 = [[UIViewController alloc] init];
    tab2.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1];
    
    // Now create an instance of our rotating tab bar controller
    RotatingTabBarController *tbc = [[RotatingTabBarController alloc] init];
    // Add the two view controllers to the tab bar
    [tbc setViewControllers:[NSArray arrayWithObjects:tab1, tab2, nil]];
    
    // Add the tab bar controller’s view to the window
    [window addSubview:tbc.view];
    
    // Make our program visible
    [window makeKeyAndVisible];
}

- (void)dealloc {
    [window release];
    [super dealloc];
}

@end

RotatingTabBarController.h

#import <UIKit/UIKit.h>

/*
 The subclass doesn’t need any new methods or members.
 */

@interface RotatingTabBarController : UITabBarController {

}

@end

RotatingTabBarController.m

#import "RotatingTabBarController.h"

@implementation RotatingTabBarController

/*
 Just override this single method to return YES when we want it to.
 */

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Always returning YES means the view will rotate to accomodate any orientation.
    return YES;
}

@end

The one thing this doesn’t address, is when you only want certain views in your program to rotate. In that case you just need to make your UITabBarController ask the currently visible view controller in the program if the app should rotate, and return that BOOL.

Share