我试着改变我的标签栏项目的颜色,因为它总是在非事件状态下是灰色的,在事件状态下是蓝色的。 因此,经过一番搜索后,我尝试将这段代码写在我的所有 ViewControllers 标签栏上
self.tabBarItem.selectedImage = [[UIImage imageNamed:@"TabBarItemMenu_tabbed.png"]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem.image = [[UIImage imageNamed:@"TabBarItemMenu.png"]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
但这对我没有帮助,我总是得到
最佳答案
您还可以这样做:
设置标签栏的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];
关于ios - 更改非事件选项卡栏图标的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25861319/