iNVASIVE CODE

Engineering and Training

How to rotate a UITabBarController

Posted by geppyp on October 29, 2008

I spent quite some time to try to understand how to rotate an application containing a tab bar controller. If you create a simple project (you can use the template Tab Bar Application offered in Xcode), you will not able to rotate the view as reported in the View Controller User Guide by Apple. I found the solution and I share it with you.

Let’s create a simple project. Open Xcode and chose the Tab Bar Application template in the iPhone OS group. Name it SimpleTab and save it where ever you like in your HD. Build and run it. If you rotate the iPhone (or the Simulator via Apple-Left or Right Arrow keys), it does not work: the interface keeps its portrait position.

Let’s add a new class to the project. Right-click the Classes group in the Groups & Files pane and choose Add… -> New File… Choose an NSObject subclass in the Cocoa Touch Classes group and name it MyTabBarController.

Open the MyTabBarController.h file and change it in this way:

#import <UIKit/UIKit.h>

@interface MyTabBarController : UITabBarController {

}
@end
 
Notice that I am subclassing UITabBarController. Now, edit the MyTabBarController.m file and add the following method:

– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}

Save it. We have simply created a subclass of UITabBarController able to rotate. Now, we need to tell Interface Builder to use this class, instead of its parent.

Double-click the MainWindow.xib file and select the Tab Bar Controller object in the MainWindow.xib window. Open the Identity Inspector and change the class name to MyTabBarController. Save it and close Interface Builder. Go back to Xcode and build and run. Now, if you try to rotate the view…… it works.

When you rotate the iPhone, the content of each view is not correctly displayed. You have to play with the autosizing mask in Interface Builder or programmatically. But I leave this to you.

Advertisements

12 Responses to “How to rotate a UITabBarController”

  1. anonymous said

    Hey thanks I was looking all over for this.

  2. […] found this example, has all the details: How to rotate a UITabBarController « iNVASIVE CODE It seems to work but some of the views in my tab bar go black. Probably I need to do something to […]

  3. […] How to rotate a UITabBarController – […]

  4. koti said

    hi,

    i am new to Iphone development, i am using TabbarController, havin 2 Toolbars, when i Rotate the Tabcontroller, the view is NOT having Toolbar,

    Plz Help. i am using IB for Toolbar,

    Thanks,
    Koti

  5. maher said

    thank you vary match its helpful relay i like your solution

    its not in apple sdk

    my bast regards
    maher

  6. Amit said

    Hello

    Nice article. I have a toolbar i want it to rotate accordingly will you please guide me how i can do it?

    Regards
    Amit

  7. RJ said

    This worked great for me in OS 3.1.2 simulator AND on my device BUT the console throws this message: Using two-stage rotation animation. To use the smoother single-stage animation, this application must remove two-stage method implementations. I’d like to make this ominous message go away even though I am satisfied with my rotation.

    So, being a novice and after doing some blog reading, I still don’t have a working solution. I know that I’m supposed to override:
    – (void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

    and instead use:
    – (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration

    So I tried to do that in my TabBarRotationController.m file where the rotation is handled BUT I still get the ominous message.

    So here’s the punt…

  8. Felix said

    hi, thank you for the tutorial. I have few questions though
    1. When I placed a button on the first view (using IB) and run the apps, the button did not display. When I put back the class to FirstViewController. The button is there but cant rotate.
    2. Do you have a sample of autorotate using UINavigationBar?

  9. Thomas Drevon said

    Good one, Geppy! Thanks a lot 😀

  10. Kristian said

    Thank you. Great help, worked just fine! 🙂

  11. Adam said

    THANK YOU! I can’t believe Apple’s documentation doesn’t include this. Worked like a charm. Thank you for the thorough instructions.

  12. Tuyen Nguyen said

    Thank you very much!!! Awesome post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: