Saturday, 7 September 2013

Customize Tab Bar Background and Appearance

With the introduction of Appearance API in iOS 5, it’s much easier to custom the UI controls that gives your app a unique look and feel.



Complete code for the customization


- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Assign tab bar item with titles

    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;

    UITabBar *tabBar = tabBarController.tabBar;

    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
    UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
    UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
 
    tabBarItem1.title = @"Home";
    tabBarItem2.title = @"Maps";
    tabBarItem3.title = @"My Plan";
    tabBarItem4.title = @"Settings";
 
    [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"home_selected.png"]
 withFinishedUnselectedImage:[UIImage imageNamed:@"home.png"]];

    [tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"maps_selected.png"]
 withFinishedUnselectedImage:[UIImage imageNamed:@"maps.png"]];

    [tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"myplan_selected.png"]
 withFinishedUnselectedImage:[UIImage imageNamed:@"myplan.png"]];

    [tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:@"settings_selected.png"]
 withFinishedUnselectedImage:[UIImage imageNamed:@"settings.png"]];

 
    // Change the tab bar background

    UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];

    [[UITabBar appearance] setBackgroundImage:tabBarBackground];

    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected.png"]];
 
    // Change the title color of tab bar items

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];

    UIColor *titleHighlightedColor = [UIColor colorWithRed:153/255.0 green:192/255.0 blue:48/255.0 alpha:1.0];

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:titleHighlightedColor, UITextAttributeTextColor,nil] forState:UIControlStateHighlighted];


   return YES;
}








No comments:

Post a Comment