简体   繁体   中英

Change color of inactive tab-bar icons

I try to change color of my tab bar items, because it always grey in unactive and blue in active. So, after some searching I try to write this code it all my ViewControllers for Tab bar

 self.tabBarItem.selectedImage = [[UIImage imageNamed:@"TabBarItemMenu_tabbed.png"]
                                     imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    self.tabBarItem.image = [[UIImage imageNamed:@"TabBarItemMenu.png"]
                             imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

But it doesn't help me, and I always get 在此处输入图片说明

You can do this in addition:

Set tintColor attribute of the tab bar to set the color of the selected icon

self.tabBar.tintColor = [UIColor redColor];

Then you can use text attributes to recolor the text

for (UITabBarItem *item in self.tabBar.items) {
    NSDictionary *normalState = @{UITextAttributeTextColor : [UIColor colorWithWhite:1.000 alpha:1.000],
                              UITextAttributeTextShadowColor: [UIColor clearColor],
                              UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0, 1.0)]};
    [item setTitleTextAttributes:normalState forState:UIControlStateNormal];

    NSDictionary *selectedState = @{UITextAttributeTextColor : [UIColor redColor],
                                UITextAttributeTextShadowColor: [UIColor clearColor],
                                UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0, 1.0)]};
    [item setTitleTextAttributes:selectedState forState:UIControlStateHighlighted];
}

// Edit

Since upper code is deprecated for iOS7, here an update:

[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                   [UIColor whiteColor], NSForegroundColorAttributeName,
                                                   nil] forState:UIControlStateNormal];

[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                   [UIColor redColor], NSForegroundColorAttributeName,
                                                   nil] forState:UIControlStateSelected];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM