Search code examples
iosuitabbarcontrolleruicolor

iOS UITabBarController "Configure" icon color


I am using a UITabBarController and I have 7 tabs, such that the "more" tab is displayed in the UITabBarController. I am trying to change the color to green for all of the selected titles and icons from the UITabBar, but in the configure screen, I only managed to change the title color - the icons remain in blue. How can I change the remaining blue color to green?

Here are my customizations in the AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [...]

    [[UITabBar appearance] setTintColor:[MyColor lightGreenColor]];
    [[UITabBar appearance] setSelectedImageTintColor:[MyColor lightGreenColor]];
    [[UINavigationBar appearance] setTintColor:[MyColor lightGreenColor]];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [MyColor lightGreenColor] } forState:UIControlStateSelected];

    return YES;
}

For better comprehension, here is a screenshot.


EDIT

I use the approach of Sen and combine it together with a UIImage category from here. In addition to that, I had to give each Tab Bar View own Tag Id, such that I can use it as an index for the icon image array. That way I do not have to manually color the icons in the green color and so, I use a quite efficient workaround for the illustrated problem. Here is the snippet, that I am using in my AppDelegate.m. I am looking forward to improvements by the community!

UIViewController *mainViewController = self.window.rootViewController;
if ([mainViewController isKindOfClass:[UITabBarController class]]) {
    UITabBarController *tabBarController = (UITabBarController *) mainViewController;

    if (tabBarController && tabBarController.tabBar &&
        [tabBarController.tabBar items] && [[tabBarController.tabBar items] count]) {
        NSArray *tabIcons = @[@"ICON1", @"ICON2", @"ICON3", ...];

        for (UITabBarItem *tabBarItem in [tabBarController.tabBar items]) {
            if (tabBarItem.tag < [tabIcons count]) {
                [tabBarItem setSelectedImage:[[UIImage imageNamed:[tabIcons objectAtIndex:tabBarItem.tag]
                                                        withColor:LIGHT_GREEN_COLOR]
                      imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
            }
        }
    }
}

Solution

  • You should have images with the right colors, and then set the images of the icons

    // unselected
    [tabBarItem setImage: [img1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    // selected
    [tabBarItem setSelectedImage: [img2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    

    Do it in application:didFinishLaunchingWithOptions:. You can get you tab bar view controller by

    UITabBarController * tc = (UITabBarController *)self.window.rootViewController;
    

    You can get the ith item by

    UITabBarItem *tabBarItem = [tc.tabBar items][i];