ios - 更改非事件选项卡栏图标的颜色

标签 ios uiimage uitabbarcontroller

我试着改变我的标签栏项目的颜色,因为它总是在非事件状态下是灰色的,在事件状态下是蓝色的。 因此,经过一番搜索后,我尝试将这段代码写在我的所有 ViewControllers 标签栏上

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

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

但这对我没有帮助,我总是得到 enter image description here

最佳答案

您还可以这样做:

设置标签栏的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/

相关文章:

swift - 如何设置标签栏徽章?

ios - 为什么我应该将我的应用程序升级到 iOS 5?

ios - 无法在 Xcode 4.3.2 中构建,因为退出代码 254 导致 clang 失败

ios - 我在 UIImageView 中添加图像但它不显示

ios - "Inverse"来自 NSAttributedString 的 UIImage

ios - 标签栏 Controller : Orienting views in different orientations

ios - cocos2d 中 Sprite 的触摸 Action

ios - subview Controller 的 viewDidLayout 在父 View Controller 的 viewDidLayout 之前调用

swift - 如何以编程方式在 SWIFT 中设置 UIImage 的 alpha?

ios - 检测选定 UITabbarItem 上的重新制表符