Author Topic: Custom tab bar item icons  (Read 19995 times)

rtamesis

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Custom tab bar item icons
« on: September 11, 2013, 04:36:48 PM »
I have a tab bar based app that I'm trying to get ready for iOS 7 and I'm trying to figure out how to change the icon when the tab bar item is selected into that of a custom icon. I can set the unselected icon image for a particular viewcontroller's bar item in the storyboard easily, but trying to get Xcode to use the custom selected icon instead of just coloring the unselected icon is proving to be more tricky. I can get it to work by going to my AppDelegate.m and inside - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions, I I can put this code in:
Code: [Select]
// Customize the tab bar icons
    UITabBarController* tabBarController = (UITabBarController *)[splitViewController.viewControllers objectAtIndex:0];
   
    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.selectedImage = [UIImage imageNamed:@"info-selected.png"];

    tabBarItem2.selectedImage = [UIImage imageNamed:@"lightwave-selected.png"];

    tabBarItem3.selectedImage = [UIImage imageNamed:@"Geometric-selected.png"];

    tabBarItem4.selectedImage = [UIImage imageNamed:@"Clinical-selected.png"];

I want to ask you, though, if there is a way of accomplishing this without going through the AppDelegate and instead do it directly in the implementation file of the viewController corresponding to each tab, say through viewDidLoad or some init method.

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
Re: Custom tab bar item icons
« Reply #1 on: September 13, 2013, 07:03:06 PM »
You can grab a reference to the tab bar controller within a view controller like this:

UITabBarController *tabBarController = self.tabBarController;

That said, I think you're fine setting the code at the app delegate level.

All the best!
Kevin

rtamesis

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: Custom tab bar item icons
« Reply #2 on: September 14, 2013, 01:55:54 AM »
Thank you! That provided me the clue that I needed to make it work from the individual viewControllers controlled by the tab bar controller. I never would have figured this out on my own even after reading Apple's documentation multiples times and going through stackoverflow.com. For others interested in implementing this, I just added these two lines to my viewDidLoad method in each of the viewControllers:

Code: [Select]
self.tabBarItem = [self.tabBarController.tabBar.items objectAtIndex:0];
    self.tabBarItem.selectedImage = [UIImage imageNamed:@"info-selected.png"];

I just changed the index number of objectAtIndex to match that of the viewController's order in the tabs and then changed the name of the selected image in the second line to match my custom selected image for that particular tab, so for the second viewcontroller, the line above was modified to:

Code: [Select]
self.tabBarItem = [self.tabBarController.tabBar.items objectAtIndex:1];
    self.tabBarItem.selectedImage = [UIImage imageNamed:@"lightwave-selected.png"];

Thank you again for your valuable advice!
« Last Edit: September 14, 2013, 02:09:12 AM by rtamesis »