更改标签栏项目图像和文本颜色iOS [英] Changing tab bar item image and text color iOS

查看:95
本文介绍了更改标签栏项目图像和文本颜色iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的标签栏:

下图显示正在运行的程序和选择的NEWS项目:

The following image shows the program being run and the "NEWS" item selected:

很明显吧色调颜色正常工作!

It is clear the bar tint color is working fine as I want !

但tintColor只影响图像而不影响文本。

But the tintColor only affects the image and not the text.

此外,当选择一个项目时(如上所示,新闻),项目颜色变为蓝色!我该如何防止这种情况发生?我希望它保持白色。

Also, when the an item is selected (as seen above, news) the item color goes blue! How do I prevent this from happening? I want it to stay white.

为什么文本在选中时会变为白色但在未选中时会变为白色?

Why is the text changing to a white color when selected but not when it is unselected?

我基本上希望项目颜色和文字颜色始终为白色。

I basically want the item color and text color to be white all the time.

我如何实现这一目标?感谢您的帮助。

How do I achieve this? Thanks for any help.

是否需要每个项目的快速代码?

Does it require swift code for each individual item?

编辑:

推荐答案

来自UITabBarItem类docs:

From UITabBarItem class docs:


默认情况下,实际未选中和所选图像是从源图像中的alpha值自动创建的
。要
防止系统着色,用
提供图像UIImageRenderingModeAlwaysOriginal。

By default, the actual unselected and selected images are automatically created from the alpha values in the source images. To prevent system coloring, provide images with UIImageRenderingModeAlwaysOriginal.

线索不是你是否使用UIImageRenderingModeAlwaysOriginal,重要的是何时使用它。

The clue is not whether you use UIImageRenderingModeAlwaysOriginal, the important thing is when to use it.

要防止未选择项目的灰色,您只需要防止未选择图像的系统着色。以下是如何执行此操作:

To prevent the grey color for unselected items, you will just need to prevent the system colouring for the unselected image. Here is how to do this:

var firstViewController:UIViewController = UIViewController()
// The following statement is what you need
var customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "YOUR_IMAGE_NAME")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "YOUR_IMAGE_NAME"))
firstViewController.tabBarItem = customTabBarItem

如你所见,我要求iOS应用原始颜色(白色)只有UNSELECTED状态的图像,黄色,红色等等,并将图像保留为SELECTED状态。

As you can see, I asked iOS to apply the original color (white, yellow, red, whatever) of the image ONLY for the UNSELECTED state, and leave the image as it is for the SELECTED state.

此外,您可能需要添加标签栏的色调颜色,以便为SELECTED状态应用不同的颜色(而不是默认的iOS蓝色)。根据上面的截图,您为所选状态应用白色:

Also, you may need to add a tint color for the tab bar in order to apply a different color for the SELECTED state (instead of the default iOS blue color). As per your screenshot above, you are applying white color for the selected state:

self.tabBar.tintColor = UIColor.whiteColor()

编辑:

这篇关于更改标签栏项目图像和文本颜色iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆