Is there a way to use a custom selected image for a UITabBarItem?

I like to have a custom image selected when the user selects an item in the tab bar, by default, he selects blue, but instead wants to have green. something like below any thoughts?

alt text http://www.freeimagehosting.net/uploads/11a2137011.png

+8
iphone uitabbar uitabbaritem
Aug 11 '10 at 19:04
source share
9 answers

Just found my solution. I basically subclassed UITabItem and installed it in the navigation controller:

-(void) viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; CustomTabBarItem *tabItem = [[CustomTabBarItem alloc] initWithTitle:@"Events" image:[UIImage imageNamed:@"tabIcon.png"] tag:0]; tabItem.customHighlightedImage=[UIImage imageNamed:@"tabIconSelected.png"]; self.tabBarItem = tabItem; [tabItem release]; tabItem=nil; } 

Here's the CustomTabBarItem class:

 @interface CustomTabBarItem : UITabBarItem { UIImage *customHighlightedImage; } @property (nonatomic, retain) UIImage *customHighlightedImage; @end 

implementation:

 #import "CustomTabBarItem.h @implementation CustomTabBarItem @synthesize customHighlightedImage; - (void)dealloc { [customHighlightedImage release]; customHighlightedImage=nil; [super dealloc]; } -(UIImage *)selectedImage { return self.customHighlightedImage; } @end 
+11
Sep 21 '10 at 16:59
source share

In iOS 6, I changed the selected tabbatitem image as -

in the tablet controller delegate method

 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { if([tabBarController selectedIndex] == 0) { [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]]; } } 

through this you can change your image.

Or you can use init (or ViewWillAppear) controllers directly in your view, for example

  [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]]; 

Hope this helps you.

+6
Feb 28 '13 at 10:49
source share

This is not officially supported in the SDK. You may be able to examine and adjust the tabs at runtime, but you run the risk of abandoning Apple.

Edit: For completeness, I should mention that your other option is to run your own UITabBar.

+2
Aug 11 '10 at 19:12
source share

Just add some custom views (using insertSubview: atIndex :) when the UITabBarController-delegate method is called.

Example:

 – (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { [tabBarController.tabBar insertSubview:someView atIndex:someIndex]; } 

You can try to change someIndex yourself until you get the desired result.

+2
Aug 11 '10 at 20:13
source share

For iOS5 and above, you can simply do this:

 rootTabBarController.tabBar.selectedImageTintColor = [UIColor greenColor]; 
+1
Jul 05 2018-12-12T00:
source share

I believe you can now do this with

 [[[[self tabBar] items] objectAtIndex:0] setFinishedSelectedImage:nil withFinishedUnselectedImage:nil]; 
+1
Jul 23 2018-12-12T00:
source share

When using storyboards, you can simply select the TabBarController TabBar, and then change the hue of the image in the Identity Inspector. This should also work with XIB.

Check out the todos image here.

+1
Mar 13 '13 at 0:14
source share

In AppDelegate.m

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions { [[UITabBar appearance] setSelectedImageTintColor:[UIColor redColor]]; return YES; } 

This will give you a red color, change the color with the one you want whiteColor, blueColor, etc.

0
Nov 19 '12 at 14:21
source share

In my UITabBarController viewDidLoad: based on Rizzu's answer:

 for (int i = 0; i < [self.viewControllers count]; i++) { UIViewController* viewController = [self.viewControllers objectAtIndex:i]; if(i == 0) { [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"btn_list_all_hover.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"btn_list_all.png"]]; } else if(i == 1) { [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"btn_settings_hover.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"btn_settings.png"]]; } } 
0
May 08 '13 at
source share



All Articles