Change iPhone/iPod app orientation within a UITabBarController

September 4th, 2008 Arash Posted in Tutorials, iPhone |

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.

3 Responses to “Change iPhone/iPod app orientation within a UITabBarController”

  1. Thanks for this tutorial, it was really simple, straightforward and very helpful!

    However, how can i change the frame of a table view so that it fills the screen when it rotates, and also is there a way of hiding the tab bar so that the table view can fill the whole screen when rotated?

    Thanks

  2. @dandle

    UIViewController has:
    @property(nonatomic) BOOL hidesBottomBarWhenPushed

    Just set that to YES and the tab bar will disappear.

    As for filling the screen, view frames automatically get changed/updated when the phone is rotated.

  3. ah, wish that were true. Some of my UITableViews get re-framed. Others do not. I have no idea why as they are set up with resizing masks and such. The documentation is certainly less than stellar. I also will add that I think some of it is wrong. In addition, I believe the sdk platform is full of bugs.

    Thanks for this insight. I may try to crawl down this path to accomplish what seems like such a simple, simple thing - hide the tab bar when a certain view is focused so that the user cannot navigate to the table views that refuse to resize themselves in landscape. Grrrrrrr.

    :) I am happy.. happy happy happy. Think happy thoughts

Leave a Reply