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

时间:2014-09-16 05:35:58

标签: ios uiimage uitabbarcontroller

我尝试更改标签栏项目的颜色,因为它始终处于非活动状态且处于活动状态时呈蓝色。 因此,经过一些搜索后,我尝试将此代码编写为我的ViewControllers for Tab bar

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

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

但它对我没有帮助,而且我总能得到 enter image description here

1 个答案:

答案 0 :(得分:1)

您可以另外执行此操作:

设置标签栏的tintColor属性以设置所选图标的颜色

self.tabBar.tintColor = [UIColor redColor];

然后您可以使用文本属性重新着色文本

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];
}

//编辑

由于iOS7不推荐使用高级代码,因此这里有更新:

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

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