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.
Like this:
This entry was posted on October 29, 2008 at 11:49 am and is filed under Cocoa Touch, iPhone. Tagged: iPhone, UITabBarController. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
anonymous said
Hey thanks I was looking all over for this.
UITabBar and autorotate - iPhone Dev SDK Forum said
[...] 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 [...]
Bookmarks for November 26th through December 1st said
[...] How to rotate a UITabBarController – [...]
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
maher said
thank you vary match its helpful relay i like your solution
its not in apple sdk
my bast regards
maher
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
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…
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?
Thomas Drevon said
Good one, Geppy! Thanks a lot
Kristian said
Thank you. Great help, worked just fine!
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.
Tuyen Nguyen said
Thank you very much!!! Awesome post.