Monday, June 10, 2013

UITabBar setSelectedImageTintColor example in Objective C (iOS).

UITabBar setSelectedImageTintColor

The tint color to apply to the gradient image used when creating the selected image.

@property(nonatomic, retain) UIColor *selectedImageTintColor

Discussion of [UITabBar setSelectedImageTintColor]
The default value is nil, which results in the system bright blue being used for selected tab item images.

If you want to also customize the unselected image appearance, you must sent setFinishedSelectedImage:withFinishedUnselectedImage: to individual tab bar items.

UITabBar setSelectedImageTintColor example.
// custom icons
UITabBarItem *item = [[UITabBarItem alloc] init];
item.title = @"foo";
// setting custom images prevents the OS from applying a tint color
[item setFinishedSelectedImage:[UIImage imageNamed:@"tab1_active.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab1_image_deselected.png"]];
tab1ViewController.tabBarItem = item;

    // tab bar

    // set background image - will be used instead of glossy black
    tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tab_bar_bg.png"];
    // optionally set the tint color - setting this ti nil will result in the standard, blue tint color. tint color is ignored when custom icons are set as above.
    tabBarController.tabBar.selectedImageTintColor = nil;
    // remove the highlight around the selected tab - or provide an alternate highlight image. If you don't do this the iOS default is to draw a highlighted box beneath the selected tab icon.
    tabBarController.tabBar.selectionIndicatorImage = [[UIImage alloc] init];

Example of [UITabBar setSelectedImageTintColor].
For iOS5 and upwards, you can just do this:

[rootTabBarController.tabBar setSelectedImageTintColor:[UIColor greenColor]];

UITabBar setSelectedImageTintColor example.
In AppDelegate.m

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

End of UITabBar setSelectedImageTintColor example article.