UITabBar 色调颜色不适用于图像

Posted

技术标签:

【中文标题】UITabBar 色调颜色不适用于图像【英文标题】:UITabBar tint color not working on images 【发布时间】:2015-01-05 21:52:42 【问题描述】:

我使用 Interface Buider 设置了 UITabBar,并且只有在我不设置全局应用程序色调颜色时,图像突出显示才能正常工作:

当我使用

设置我的应用的全局色调颜色时
[[UIView appearance] setTintColor:[TAStyleKit tintColor]];

然后所有选项卡图像都显示为选中状态。

只有当我单击选项卡并再次返回时,它们才具有正确的颜色。请注意此处为灰色的“武器”选项卡:

我做错了什么?

【问题讨论】:

【参考方案1】:

编辑:别管我之前写的,看来你只需要更改以下内容...

[[UIView appearance] setTintColor:[TAStyleKit tintColor]];

到...

[[UITabBar appearance] setTintColor:[TAStyleKit tintColor]];

请注意,您正在尝试更改 UIView 而不是 UITabBar 的外观。我在一个项目中运行了它,它成功了。

Swift 4.0

UITabBar.appearance().tintColor = UIColor.redTAStyleKit.tintColor

【讨论】:

我每个标签只有一张图像,我确实希望将色调应用于图像 我删除了我写的所有东西,并写了一个解决方案,应该可以完成你正在寻找的东西。您正在设置 UIView 而不是 UITabBar 的外观。 我在 UIView 上设置色调,因为我希望我的所有 ui 元素都具有这种色调,而不是默认的蓝色。 我给你的建议,虽然不是最方便的,但是在你所有支持 UIAppearance 的视图(主要是 UIView 子类)上设置全局色调颜色。 selectedImageTintColor 存在一个尚未解决的已知问题,这是您在设置 UIView 色调颜色时看到的。因此,如果您只有一个标签栏、导航栏、分段控件等......它应该只在您的代码中添加几行。希望这会有所帮助。【参考方案2】:

当您不能或不想删除基本的 UIView.tintColor 代码时,这里可能会更好。只需将标签栏内 UIViews 上的 tintColor 设置为默认灰色即可。

let view = UIView.appearance()
view.tintColor = UIColor.redColor()

let viewsInTabBar = UIView.appearanceWhenContainedInInstancesOfClasses([UITabBar.self])
viewsInTabBar.tintColor = UIColor(white: 144.0 / 255.0, alpha: 1) // default gray for inactive items

let tabBar = UITabBar.appearance()
tabBar.tintColor = UIColor.redColor() // actual highlight color

不幸的是,苹果没有使用其标准灰度值之一...

【讨论】:

【参考方案3】:

也可以直接从 Storyboard[XCode 10.1] 通过设置 UITabBar 的 Image Tint 来设置

【讨论】:

以上是关于UITabBar 色调颜色不适用于图像的主要内容,如果未能解决你的问题,请参考以下文章

UITabBar 选择的色调颜色

UITabBar 所选项目色调颜色

在 UITabBar 中为选定选项卡设置色调颜色

UITabBar 选择的选项卡在启动时不会改变色调颜色

设置选定的标签栏项目色调?

UINavigationBar backgroundColor 不适用于 UITabBar