MayurCM
MayurCM

Reputation: 701

How to set an image on tab bar controllers?

I am trying to change the image of a Tab Bar in my application. When I changed the image it was giving me a blank image instead.

Upvotes: 1

Views: 3383

Answers (3)

Hubert Kunnemeyer
Hubert Kunnemeyer

Reputation: 2261

in iOS5 all of those things are possible!

UIImage *selectedImage0 = [UIImage imageNamed:@"TabBa1selected.png"];
     UIImage *selectedImage1 = [UIImage imageNamed:@"TabBa2selected.png.png"];
     UIImage *selectedImage2 = [UIImage imageNamed:@"TabBa3selected.png.png"];

    UITabBar *tabBar = self.tabBarController.tabBar;
    UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
    UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
    UITabBarItem *item2 = [tabBar.items objectAtIndex:2];

    [item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:selectedImage0];
    [item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:selectedImage1];
    [item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:selectedImage2];
    UIImage* tabBarBackground = [UIImage imageNamed:@"tabBarBackground.png"];
    UIImage* tabBarSelected = [UIImage imageNamed:@"SelectedImage.png"];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];
    [[UITabBar appearance] setSelectionIndicatorImage:tabBarSelected];

Upvotes: 3

Ryan Crews
Ryan Crews

Reputation: 3033

You would typically set an image for your tab bar in the AppDelegate class. It likely has a tab bar controller defined as _tabBarController, so the code would be:

[[_tabBarController tabBar] setBackgroundImage:[UIImage imageNamed:@"imageName.png"]];

If the image provided is of the proper size or stretchable it will be that image, otherwise it will be the image tiled however many times it takes to fill the tab bar.

You could also change the tab color if you'd like.

Upvotes: 1

Anand
Anand

Reputation: 3406

No, You can use .icon images for tabbar which has transparent background and black and white. It is possible to set color of tabbar also.

tabbar Color

     CGRect frame = CGRectMake(0.0, 0, 320, 48);
 UIView *v = [[UIView alloc] initWithFrame:frame];
 [v setBackgroundColor:[UIColor colorWithRed:0.2 green:0.8 blue:0.4 alpha:0.3]]; 
//[v setAlpha:1.0];
 [[tabbar tabBar] insertSubview:v atIndex:0];
 [v release];

Upvotes: 3

Related Questions