Friday, January 6, 2012

Custom tab bar in iphone


Tab bar in iPhone application is a desirable control when your application has multiple section and subsections. It takes viewControllers to create the tabs and display on the view/window. The default color of tab bar controller is black.

With introduction of iOS 5.0, we can change its tint color using,
tabBar.tintColor=[UIColor greenColor];

But, this is not enough to satisfy our taste buds. We are not at all going to create a custom tab bar, any inheritance etcs. We are just going to have some Image layers put onto the tab bar. With a little tweak, we can use whatever image we like in tab bar.
For this we will need a set of Image for every tab like,







Now, we will need to initialize our view controller in "didFinishLaunchingWithOptions" method as below,

FirstViewController *firstViewController=[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:[NSBundle mainBundle]];
..
..
FifthViewController *fifthViewController=[[FifthViewController alloc] initWithNibName:@"FifthViewController" bundle:[NSBundle mainBundle]];


and add to tab bar controller,
    NSArray *arr=[NSArray arrayWithObjects:firstViewController,secondViewController,thirdViewController,fourthViewController,fifthViewController,nil];
    tabBarController=[[UITabBarController alloc] init];
    [tabBarController setViewControllers:arr];


Finally add this tab bar controller to the window,
[self.window addSubview:tabBarController.view];

Complete "didFinishLaunchingWithOptions" method in AppDelegate.m of application will typically look like below,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    
    FirstViewController *firstViewController=[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:[NSBundle mainBundle]];
    
    SecondViewController *secondViewController=[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
    
    ThirdViewController *thirdViewController=[[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:[NSBundle mainBundle]];
    
    FourthViewController *fourthViewController=[[FourthViewController alloc] initWithNibName:@"FourthViewController" bundle:[NSBundle mainBundle]];
    
    FifthViewController *fifthViewController=[[FifthViewController alloc] initWithNibName:@"FifthViewController" bundle:[NSBundle mainBundle]];
    
    NSArray *arr=[NSArray arrayWithObjects:firstViewController,secondViewController,thirdViewController,fourthViewController,fifthViewController,nil];
    tabBarController=[[UITabBarController alloc] init];
    [tabBarController setViewControllers:arr];
    [self.window addSubview:tabBarController.view];
    
    [self.window makeKeyAndVisible];
    return YES;
}

Now, the idea is that, we can add an ImageView over the tab bar and update the image with tab change in viewWillAppear method of every viewController as below,
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
for(UIView *view in self.tabBarController.tabBar.subviews) {
if([view isKindOfClass:[UIImageView class]]) {
[view removeFromSuperview];
}
}
[self.tabBarController.tabBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]] autorelease] atIndex:1];
}

You can download the example code from here.