Creating a UITabBarController

I haven’t seen many examples of creating a tabbar controller other than “theElements” example from Apple, which is pretty intense. So, what follows is a short snippet from a proof-of-concept application I was recently working on.

The basic idea is as follows: a UITabBarController contains an array of UIViewController objects, and these views are swapped when one of the tabbar buttons is clicked. You can see in the code below how I setup the view controllers that will be displayed for each tab, and add those controllers into the tabbar controller array.

The resulting tabbar looks as follows:

The interface file

    //
    //  BirdBookAppDelegate.h
    //
    //  Created by john on 8/5/08.
    //  Copyright iPhoneDeveloperTips.com 2008. All rights reserved.
    //
    #import <UIKit/UIKit.h>

    @interface BirdBookAppDelegate : NSObject
    {
      UINavigationController *navigationController;
      UIWindow *portraitWindow;
      UITabBarController *tabBarController;
    }

    @property (nonatomic, retain) UINavigationController *navigationController;
    @property (nonatomic, retain) UITabBarController *tabBarController;
    @property (nonatomic, retain) UIWindow *portraitWindow;

    @end

The implementation file

    //
    //  BirdBookAppDelegate.m
    //
    //  Created by john on 8/5/08.
    //  Copyright iPhoneDeveloperTips.com 2008. All rights reserved.
    //

    #import "BirdBookAppDelegate.h"
    #import "Bird.h"
    #import "AllBirds.h"

    @implementation BirdBookAppDelegate

    @synthesize navigationController;
    @synthesize tabBarController;
    @synthesize portraitWindow;

    /*************************************************************************/
    - (void) configureUI
    {
      // Create nav-controller for local use
      UINavigationController *localNavController;

      // Create portrait window and content view (to hold other views)
      UIWindow *localPortraitWindow;
      localPortraitWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
      self.portraitWindow = localPortraitWindow;
      [localPortraitWindow release];  // Retained by delegate thru above setter

      // Set background color
      [portraitWindow setBackgroundColor:[UIColor blackColor]];

      // New tabbar controller and array to contain the view controllers
      // There are two (2) view controllers needed
      // One for "Identify Bird" and one for "Lookup Bird"
      tabBarController = [[UITabBarController alloc] init];
      NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:2];

      /*--------------------------------------------------------------------
       * Setup the 2 view controllers for the different data representations
       *-------------------------------------------------------------------*/

      // Root view controller for "Identify Bird"
      IdentifyBirdRootViewController *vc;
      vc = [[IdentifyBirdRootViewController alloc] initWithStyle:UITableViewStylePlain];
      localNavController = [[UINavigationController alloc] initWithRootViewController:vc];
      localNavController.tabBarItem.image = [UIImage imageNamed:@"123.png"];
      [vc release];   // This is now managed by the navigation controller

      // Add navigation controller to the local vc array (1 of 2)
      [localViewControllersArray addObject:localNavController];
      [localNavController release]; // Retained by above array

      // Root view controller for "Lookup Bird"
      vc = [[LookupBirdRootViewController alloc] initWithStyle:UITableViewStyleGrouped];
      localNavController = [[UINavigationController alloc] initWithRootViewController:vc];
      localNavController.tabBarItem.image = [UIImage imageNamed:@"ABC.png"];
      [vc release];   // This is now managed by the navigation controller 

      // Add navigation controller to the local vc array (2 of 2)
      [localViewControllersArray addObject:localNavController];
      [localNavController release]; // Retained by above array

      // Point the tab bar controllers view controller array to the array
      // of view controllers we just populated
      tabBarController.viewControllers = localViewControllersArray;
      [localViewControllersArray release]; // Retained thru above setter

      // Add subview to portrait window to reference the tabbarcontroller
      [portraitWindow addSubview:tabBarController.view];

      // Show it
      [portraitWindow makeKeyAndVisible];
    }

    /*************************************************************************/
    - (void) applicationDidFinishLaunching:(UIApplication *)application
    {
      // Configure the user interface (tabbar, nav controllers, etc)
      [self configureUI];
    }

    /*************************************************************************/
    - (void) dealloc
    {
      [tabBarController release];
      [portraitWindow release];
      [navigationController release];
      [super dealloc];
    }

    @end

Tagged:

One thought on “Creating a UITabBarController

  1. rcloudsoftware 01/03/2009 at 12:43 AM Reply

    I’ve been searching blogs everywhere for an answer to a question I have about UITabBarController, maybe you can help? It seems you have a good grasp on how the control works.

    My question, is how do you get the size of the tab bar at the bottom? Similarly, how do you know how much room you have inside the tabbed-view for your content? I see a lot of examples of people sizing their tab child views to [[UIScreen mainScreen] applicationFrame], which can’t be correct because the tab bar takes up some room at the bottom of the screen.

    Also, is it possible to modify the size of the tabbar?

    Thanks
    Ryan

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: