简体   繁体   English

更改非活动的标签栏图标的颜色

[英]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 因此,经过一番搜索,我尝试将这段代码全部写入Tab栏的所有ViewController

 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 设置选项卡栏的tintColor属性以设置所选图标的颜色

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: 由于iOS7不建议使用上层代码,因此请进行以下更新:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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