【问题标题】:Change tintColor of unselected UITabBarController item title and background image更改未选中的 UITabBarController 项目标题和背景图像的 tintColor
【发布时间】:2014-12-20 12:00:44
【问题描述】:

如何更改未选择的 UITabBarItem 标题和背景图像 iOS 8 的 tintColor?

未选中状态的默认颜色是浅灰色,但它不会显示在我的深阴影 UITabBar 背景上

我希望我的未选中状态具有 [UIColor blackColor] 的颜色

在我的应用程序委托中,didfinishlaunchingwithoptions:我有

UIImage *deselectedE = [[UIImage imageNamed:@"mincraft_axe_green_32.png"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
UIImage *selectedE = [[UIImage imageNamed:@"mincraft_axe_green_32.png"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
e.tabBarItem =  [[UITabBarItem alloc] initWithTitle:@"Profile" image:deselectedE selectedImage:selectedE];
[[UITabBar appearance] setTintColor:[UIColor blackColor]];

【问题讨论】:

标签: ios ios8 uitabbaritem tintcolor bartintcolor


【解决方案1】:

想通了!

用它来改变文本的颜色:

[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor greenColor] }
                                         forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] }
                                         forState:UIControlStateSelected];

并确保图像的图像渲染模式设置为原始图像

UIImage *deselectedImage = [[UIImage imageNamed:@"deselectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *selectedImage = [[UIImage imageNamed:@"selectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

【讨论】:

  • 原图是个好东西。感谢分享。
  • 如果我的原始图标是黑色的但我希望它是白色的怎么办?
  • 我遇到了类似的情况,但我希望我未选择的图像为白色(原始图像为白色),而我选择的图像为黄色(当前为灰色)。如果我愿意更改所选图像的颜色,我确信他的上述解决方案有效,但我想为所选图像使用内置的“色调颜色”属性。当我尝试将我选择的图像放在选项卡栏项下的属性检查器中时,它在运行时将图像留空(错误)。我在我的应用委托中使用了这个解决方案:[link] (stackoverflow.com/questions/26515703/…)
  • @ReneeOlson 它确实感觉有点hacky,但它确实有效。很高兴它对你有所帮助:)
  • 不敢相信你必须在 iOS 8 中预渲染颜色。这倒退了一步!不过谢谢!在情节提要中实现这一点的任何方式?
【解决方案2】:

application didFinishLaunchingWithOptions: 的 AppDelegate.m 内使用以下代码:

//unselected icon tint color 
[[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor:[UIColor redColor]];

//selected tint color 
[[UITabBar appearance] setTintColor:[UIColor greenColor]];

//text tint color 
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }
                                     forState:UIControlStateNormal];

//background tint color 
[[UITabBar appearance] setBarTintColor:[UIColor blueColor]];

【讨论】:

  • 在选择该项目然后取消选择后更改未选择的色调颜色会变回来。似乎使未选中状态成为您想要的颜色的唯一方法是使用您自己的图像。你可以在这里看到如何做到这一点:stackoverflow.com/a/19662170/4114683
  • 我试过发现你也可以使用[[UIButton appearance] setTintColor:[UIColor greenColor]];来保持iOS 7/8中选定的标签栏项目和按钮标签之间的一致性(例如,像音乐应用程序)
  • 像魅力一样工作。谢谢!
  • 很遗憾,这在以后的版本中已被弃用。
  • [[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor:[UIColor redColor]];不仅已弃用,而且不起作用。
【解决方案3】:

您还可以从资产文件的属性检查器中将图像渲染为原始图像,而无需编写任何代码

【讨论】:

  • 它不是来自情节提要,而是来自资产文件的属性检查器。除此之外,这是最好的答案。
【解决方案4】:

您也可以直接在 Storyboard 中进行设置...在此处查看我的答案: How to set UITabBarItem's unselected tint, ***including system items*** (iOS7)

如果您使用 Storyboard,您还可以将 Image 设置为 Bar ItemSelected Image 设置为 Selected Bar Item 以在 tabBar 中获得不变的图像。

或者在Assets目录中,您可以在图像的属性中选择Render AsOriginal Image (View > Utilities > Show Attributes Inspector 或快捷方式⌥⌘4 (Option + Command + 4) )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    相关资源
    最近更新 更多