I like to have a custom selected image when a user selects an item on the tab bar, by default it selects as blue like but would like to have a green color instead. something like below any thoughts?
9 Answers
Just found my solution. Basically, I subclassed UITabItem and set this 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 what the CustomTabBarItem class looks like:
@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

- 3,073
- 5
- 40
- 67
-
what is customHighlightedImage? What did you put in the CustomTabBarItem class? – Ben Williams Oct 29 '10 at 04:07
-
just added more details on what CustomTabBarItem looks like – Frank Oct 29 '10 at 17:36
-
what would be the method for the unselected image please? – Tiago Aug 10 '11 at 12:22
-
I don't see `- (UIImage *)selectedImage` Anywhere in the docs for UITabBarItem. Seems to work though, so I'm gonna guess this is "Private API" and therefore fatal to 99% of projects. – SG1 Nov 06 '10 at 22:06
In iOS 6 I have change the selected tabbatitem image like -
in tabbar 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 directly in your view controllers init(or ViewWillAppear) method, like
[viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
i hope this'll help you.

- 3,470
- 1
- 27
- 35
-
1This works great, except the selected image isn't set until the tab is selected (for me the first tab is still tinted blue initially). Iterating through self.viewControllers in viewDidLoad in my UITabBarController did the trick though. – Dustin May 08 '13 at 00:00
-
I am also facing same issue as Dustin. Please let me know how can we show selected or unselected image without selecting tab manually. – Mohd Haider Nov 11 '14 at 14:42
-
I ma not sure but you can try this line of code. :- [tabBar setSelectedItem:[tabBar.items objectAtIndex:0]]; – Muhammad Rizwan Nov 12 '14 at 06:51
This is not officially supported in the SDK. You may be able to probe and adjust the tab's views at runtime, but you risk a rejection from Apple.
Edit: For completeness, I should mention that your other option is to roll your own UITabBar.

- 20,509
- 6
- 47
- 58
Just add some custom views (using insertSubview:atIndex:) when the UITabBarController-delegate-methods are called.
Example:
– (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
[tabBarController.tabBar insertSubview:someView atIndex:someIndex];
}
You can try changing someIndex
yourself till you have the result you want.

- 10,127
- 4
- 40
- 57
-
this is good because it's easy to implement and it doesn't use any private method, therefore will have no problems on app review. – Fabio Cionini Nov 10 '11 at 22:35
For iOS5 and upwards, you can just do this:
rootTabBarController.tabBar.selectedImageTintColor = [UIColor greenColor];

- 37,173
- 19
- 130
- 154
-
That set tint color of all image. I want only one tabbar. How would I do so? – user4951 Mar 18 '14 at 14:23
I believe you can now do this with :
[[[[self tabBar] items] objectAtIndex:0] setFinishedSelectedImage:nil withFinishedUnselectedImage:nil];

- 11,839
- 9
- 58
- 91
-
`setFinishedSelectedImage:withFinishedUnselectedImage:` is a method of `UITabBarItem`, not `UITabBar`. – dvs Jan 31 '13 at 18:08
When using storyboards you can simply select your TabBarController's TabBar and then change the Image Tint in the Identity Inspector. This should also work with XIBs.

- 263
- 1
- 3
- 11
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 wish whiteColor, blueColor etc..

- 1,358
- 1
- 9
- 11
In my UITabBarController's 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"]];
}
}

- 94
- 1
- 3
-
Hi Dustin. My image on UITabBarItem is not set until I manually select required UITabBarItem. If you solved this problem, then give me some hint for this. If possible then please provide me code. – Mohd Haider Nov 11 '14 at 14:40