我想更改 ios7 标签栏中非事件图标的颜色。
我知道如何为选定的 TabBar 项目设置颜色,但我不知道如何为非事件的 TabBar 项目设置颜色。
有人知道怎么做吗? 提前致谢!!
这是我在 appDelegate.m 中的代码
//tint color for tabbar
[UITabBar appearance].barTintColor = [UIColor colorWithRed:0.077 green:0.411 blue:0.672 alpha:1.000];
//tint color for the text of inactive tabbar item.
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];
//tint color for the text of selected tabbar item.
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor orangeColor]} forState:UIControlStateSelected];
//tint color for the selected tabbar item.
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
//tint color for the inactive tabbar items.
//Question:how can I set tint color for the inactive tabbar items???
最佳答案
的确,没有简单的方法可以更改非事件图像的颜色。似乎根本不可能在 Storyboard中完成。不过,有一个非常简单的解决方案 — 只需为项目分配一个具有 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal
渲染模式的图像。
鉴于您的图像最初已经为非事件状态着色,您可以使用以下实现创建 UITabBarItem
的简单自定义子类
@implementation P2TabBarItem
- (void)awakeFromNib {
[self setImage:self.image]; // calls setter below to adjust image from storyboard / nib file
}
- (void)setImage:(UIImage *)image {
[super setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
self.selectedImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
}
@end
在您的 Storyboard中,将所有 UITabBarItem 的 Class 字段填充为新创建的子类名称 — 就是这样!
关于ios - 如何更改 ios7 中非事件标签栏图标的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21566531/