how to set iphone tab bar icon by custom images using story board

31,230

Solution 1

add the Images in application,

and See attached Image

enter image description here

Solution 2

Use this code in viewDidLoad

UIImage *selectedImage0 = [UIImage imageNamed:@"selected.png"];
UIImage *unselectedImage0 = [UIImage imageNamed:@"unselected.png"];

UITabBarItem *item0 = [tabBar.items objectAtIndex:0];

[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];

Solution 3

You can use this code snippet for ios7:

UIImage *iconBoxOffice = [UIImage imageNamed:@"box-office.png"];
UIImage *selectedIconBoxOffice = [UIImage imageNamed:@"selected-box-office.png"];
UIImage *iconDvds = [UIImage imageNamed:@"dvds.png"];
UIImage *selectedIconDvds = [UIImage imageNamed:@"dvds.png"];

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

[item0 initWithTitle:@"Box Office" image:iconBoxOffice selectedImage:selectedIconBoxOffice];
[item1 initWithTitle:@"Dvds" image:iconDvds selectedImage:selectedIconDvds];

You can keep adding more items if you have more tabs. *don't forget to add the images to your project *this is done programmatically, not using storyboards

Solution 4

Use below code in AppDelegate

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary  *)launchOptions
{


UIViewController *viewController1 = [[ViewFirstViewController alloc] init];

UIViewController *viewController2 = [[ViewSecondViewController alloc] init];
UIViewController *viewController3 = [[ViewThirdViewController alloc] init];
UIViewController *viewController4 = [[ViewFourthViewController alloc] init];

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2,viewController3,viewController4, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];


imgTab = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,320.0,50)];
self.tabBarController.tabBar.tag=10;
[self.tabBarController.tabBar addSubview:imgTab];
self.tabBarController.delegate = self;

imghome=[[UIImageView alloc]initWithFrame:CGRectMake(0.0,00.0,80.0,50.0)];
imghome.image=[UIImage imageNamed:@"dressup_active.png"];
[imgTab addSubview:imghome];

imgQuiz=[[UIImageView alloc]initWithFrame:CGRectMake(80.0,00.0,81.0,50.0)];
imgQuiz.image=[UIImage imageNamed:@"x-mas_tree.png"];
[imgTab addSubview:imgQuiz];

imgtTW=[[UIImageView alloc]initWithFrame:CGRectMake(161.0,00.0,80.0,50.0)];
imgtTW.image=[UIImage imageNamed:@"greetings.png"];
[imgTab addSubview:imgtTW];

imgGuest=[[UIImageView alloc]initWithFrame:CGRectMake(241.0,00.0,80.0,50.0)];
imgGuest.image=[UIImage imageNamed:@"quotes_tab.png"];
[imgTab addSubview:imgGuest];

}

Tabbar Controller delegate method

- (void)tabBarController:(UITabBarController *)tabBarControllers didSelectViewController:(UIViewController *)viewController
{

NSLog(@"%i",tabBarControllers.selectedIndex);
if (tabBarControllers.selectedIndex == 0)
{

    imghome.image=[UIImage imageNamed:@"dressup_active.png"];
    imgQuiz.image=[UIImage imageNamed:@"x-mas_tree.png"];
    imgtTW.image=[UIImage imageNamed:@"greetings.png"];
    imgGuest.image=[UIImage imageNamed:@"quotes_tab.png"];

}
else if (tabBarControllers.selectedIndex == 1)
{

    imghome.image=[UIImage imageNamed:@"dressup.png"];
    imgQuiz.image=[UIImage imageNamed:@"x-mas_tree_active.png"];
    imgtTW.image=[UIImage imageNamed:@"greetings.png"];
    imgGuest.image=[UIImage imageNamed:@"quotes_tab.png"];

}
else if (tabBarControllers.selectedIndex == 2)
{

    imghome.image=[UIImage imageNamed:@"dressup.png"];
    imgQuiz.image=[UIImage imageNamed:@"x-mas_tree.png"];
    imgtTW.image=[UIImage imageNamed:@"greetings_active.png"];
    imgGuest.image=[UIImage imageNamed:@"quotes_tab.png"];

}
else if (tabBarControllers.selectedIndex == 3)
{

    imghome.image=[UIImage imageNamed:@"dressup.png"];
    imgQuiz.image=[UIImage imageNamed:@"x-mas_tree.png"];
    imgtTW.image=[UIImage imageNamed:@"greetings.png"];
    imgGuest.image=[UIImage imageNamed:@"quotes_active.png"];

}



}

Solution 5

If you want to add a custom image (icon and background) to the tab bar and not just change the blue icon:

Add the code below to your first UIView .m-file. If you start fresh with a "Tabbed Application" project it's called FirstViewController.m Don't forget to add the images (and set check mark 'Copy items into destination group's folder') to your project and choose better names for the images.

- (void)viewDidLoad
{
  [super viewDidLoad];

  UIImage *selectedImage0 = [UIImage imageNamed:@"customIcon0_unpressed.png"];
  UIImage *unselectedImage0 = [UIImage imageNamed:@"customIcon0_unpressed.png"];

  UIImage *selectedImage1 = [UIImage imageNamed:@"customIcon1_unpressed.png"];
  UIImage *unselectedImage1 = [UIImage imageNamed:@"customIcon1_unpressed.png"];

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

  [item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
  [item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
}

You can find more informations about this solution and tons of other great tutorials on Ray Wenderlichs page.

Share:
31,230
MohanRaj S
Author by

MohanRaj S

Anxious to learn and help to the programming community.

Updated on July 05, 2022

Comments

  • MohanRaj S
    MohanRaj S almost 2 years

    I am newbie to iphone development,I need to put a custom image icon in the tab bar .But it showing only the default colour(Black&blue) .Help me to overcome from this bug friends...?

  • Siddharth
    Siddharth over 10 years
    Where do you get tabBar variable from ?
  • Saleh Masum
    Saleh Masum over 9 years
    This method is deprecated now.
  • CRDave
    CRDave about 9 years
    @SalehMasum Thank to draw my attention on that. If you know solution please edit my answer. I will accept that.
  • iosMentalist
    iosMentalist about 9 years
    the selected images are not wokring.. i tried from the identify inspector and attributes inspector xcode >6.1
  • ashokdy
    ashokdy over 8 years
    thank you so much dude i was searching exactly for this thanks a lot +1