[Tutorial] Auto-Rotate UITabBar Based App

Discussion in 'iOS Development' started by cocotutch, Mar 21, 2009.

  1. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    This can achieved by subclassing UITabBarController.

    Start by opening a Tab Bar Application in Xcode.

    Do NOT do anything in Xcode (yet..

    Please Register or Log in to view images

    ) and open up MainWindow.xib.
    Click Tab Bar Controller and navigate to the Identity Tab and name it "MyTabBarController".
    Write the Class Files and add them to Xcode.

    Make sure MyTabBarController.h looks like this:

    Code:
    #import <UIKit/UIKit.h>
    
    @interface MyTabBarController : UITabBarController {
    
    }
    
    @end
    Make sure MyTabBarController.m looks like this:

    Code:
    #import "MyTabBarController.h"
    
    @implementation MyTabBarController
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    	return YES;
    }
    
    @end
    Now Build and Go to the Simulator from the Project Window and voila, you have an auto-rotating Tab Bar in your iPhone application!

    Peace.......cocotutch

    Please Register or Log in to view images

  2. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    you are doing it wrong!

    just set - (BOOL)shouldAutorotateToInterfaceOrientation

    Please Register or Log in to view images

    UIInterfaceOrientation)interfaceOrientation {
    return YES;
    }
    to all your view controllers that is in Tab Bar.
  3. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    I don't seem to think so, I wrote class files for all my Views and added that line, it never autorotated the tab bar as well as the view, it didn't even rotate the view.

    So, by subclassing the way I have above, it keeps that method chained to the two source files, not in every single view controller file. It kinda' keeps it neat.

    Please Register or Log in to view images



    If I had found another way to add it to View Controller Files and it worked for me, I would have posted it here.

    Thanks
  4. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Fail.

    According to Apple, you should never subclass UITabBarContriller it UINavigationController. I strongly agree with that.

    A UITabBarController will auto rotate just fine if you set it to autorotate AND set the controllers it controls to autorotate. The funny thing us that Apple says this almost
    word for word in their dogs, yet no one seems to read them.

    Also, you don't set a view to autorotate. You tell the view controller to autorotate the views it controls. That is why your initial trials were failing to rotate the views.



    EDIT: Alright, I shouldn't have to do this, but I am feeling generous. Here is a sample project showing how to auto rotate a tab bar app. I took the sample tab bar app template that is available in xcode and added a subclass for the second view controller. That however was only so I could override its autorotation function. Aside from that, the application is identical to what xcode creates for you. I then amended to view controllers by changing the autorotating function to this:
    Code:
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ? NO : YES);
    }
    Download the sample app here to see how to properly autorotate a tab bar app: http://www.touchrepo.com/SampleCode/AutorotatingTabBarApp.zip
  5. bj97301

    bj97301 New Member

    Joined:
    Aug 21, 2009
    Messages:
    1
    Likes Received:
    0
    Device:
    iPhone 3GS (Black)
    The above implementation does not seem to work when one controller rotates and the other does not. Any ideas?


  6. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    That's funny, here's a video of the Xcode project that I linked to in the post you quoted: http://www.skylarcantu.com/videos/AutorotatingTabBar/AutorotatingTabBar.mov

    As you can see, it works perfectly. So I clearly must know what I'm talking about. Instead of ignoring what I say, why not reread to figure out what you're doing wrong.

    You are asking how to get the tab controller to rotate when one view controller autorotates and the other does not. I clearly stated why.

    To not have the view controllers rotate when at least one subviewcontroller does not autorotate is preferable, as it's awkward for the user to switch orientations back and forth when they select a tab.

    That said, you can work around it by setting autorotation to YES on the viewcontroller that you don't want to rotate. You can then catch the rotation methods and manually "not rotate" your view controller. You can see this in this video: http://www.skylarcantu.com/videos/PT4LandscapeMode/PT4LandscapeMode.mov I choose to not rotate the background while rotating everything on top of it. It looks very bad because of the nature of the Simulator, but on the device it is perfect. [Skip to about half way through the video].

    One way to accomplish this is to catch which way the view controller is currently rotated and which way the view controller will be rotated. You can then rotate its view (self.view) the opposite direction of the device's actual rotation. The illusion should work flawlessly, with the background staying in place while the tab bar moves around it.
  7. ipodmail09

    ipodmail09 Member

    Joined:
    Mar 13, 2009
    Messages:
    269
    Likes Received:
    0
    Device:
    2G iPod touch
    • Immature or obscene comments
    FAIL

    Please Register or Log in to view images

  8. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    ipodmail09, Please refrain from making posts like the one you just made. It contributes nothing to the conversation and is nothing more than a distraction from the conversation.

    If you still want to point out a meaningless typo, then feel free to do so; but do it like a normal person (ie, not like a five year old).
  9. jessem

    jessem Guest

    Fail.

    Skylar, you should never use ie in an example context. eg is the appropriate abbreviation for "for example", which most people seem to clearly miss.

    I shouldn't have to do this but I googled up an example.
    http://ancienthistory.about.com/od/abbreviations/f/ievseg.htm

    Code:
    I.E. (Id Est)
    
    I'm going to the place where I work best, i.e., the coffee shop.
    [There is only one place that I am claiming is best for my work. By using "i.e.", I am telling you I am about to specify it.]
    
    E.G. (Exempli Gratia)
    
    At the places where I work well, e.g., Starbucks, I have none of the distractions I have at home.
    [There are lots of coffee shops I like, but Starbucks is the only international one, so it's the only "example" that would work.]
  10. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    a) Don't give a sh*t.
    b) In that context, ie works just fine.
    c) That's irrelevant, because the two are interchangeable in modern conversational english.

Share This Page