ios - 如何更改 ios7 中非事件标签栏图标的颜色?

标签 ios iphone objective-c ios7

我想更改 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/

相关文章:

ios - 将动态库注入(inject)企业应用程序(ipa)

ios - CBCentralManager 在 iOS 中获取广告蓝牙 LE 设备的动态列表

ios - 如何将 UIcolor 分配给 NSString?

ios - 以明文形式发送用户名/密码是否安全?

iphone - 是否可以在 iPad 上构建 iPad 应用程序?

c++ - 将旧的 Mecab 库实现到现代 iOS 应用程序中

iphone - 使用固定中心点为 UIButton 中的宽度变化设置动画

iphone - ARC 中的内存问题

iphone - 如何将 plist 从 bundle 移动到 iOS 上的文档文件夹

ios - 为什么 Xcode 不识别 PFFacebookUtils?