1

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]];
            }
        }
    }
}
Community
  • 1
  • 1

1 Answers1

0

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];
ukim
  • 2,395
  • 15
  • 22
  • Is it really the only possibility? This is quite inefficient and would cause a lot of extra workload. Why does the `UITabBarItem` not inherit the global tint? Besides, how can I combine your suggestion with my `AppDelegate.m` approach? – user3869711 Jul 23 '14 at 17:47
  • According to my experience, this is the only way. You can change the bar tint color and title color by code, but I don't think you can change the color of the icons programmatically. I have updated my answer. – ukim Jul 23 '14 at 18:21
  • Ok, thank you for your update! The only thing that confuses me is, that the other icons (in the normal, not the **more** section) are displayed correctly in green and only the ones displayed in the **configure** screen are wrong. Using the `self.window.rootViewController` approach I will iterate through all of the tab bar icons? Not only the **wrong** ones? – user3869711 Jul 24 '14 at 07:19
  • @user3869711 Do you know the index of the particular tags in the configure screen? – ukim Jul 24 '14 at 15:55
  • I don't think that the indexes will help me, since I basically have to change the images (normal/ selected) for each tab bar icon. This is due to the possibility to switch the tab bars that are shown in the "configure" screen. I hope now you can understand, why I wanted to avoid this approach and find a more efficient way. I just don't get it, why the icons in the "configure" screen are painted in a wrong color. – user3869711 Jul 25 '14 at 14:05