ios 如何明确设置 UITabBar 背景颜色和色调颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37626377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:15:18  来源:igfitidea点击:

How to definitively set UITabBar background color and tint color

iosswiftuitabbar

提问by Harry Merzin

I have been trying to set my UITabBar's tint color and background color for quite some time now and nothing seems to work. So far I have tried:

UITabBar一段时间以来,我一直在尝试设置我的色调和背景色,但似乎没有任何效果。到目前为止,我已经尝试过:

tabBarController?.tabBar.backgroundColor = UIColor.orangeColor()
tabBarController?.tabBar.barTintColor = UIColor.whiteColor()

as well as:

也:

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

Neither of these seemed to have any effect on my tab bar. I'd also like to mention that I have the VC embedded in a navigation controller for which the global tint color that I set works perfectly fine.

这些似乎都对我的标签栏没有任何影响。我还想提一下,我将 VC 嵌入到导航控制器中,我为其设置的全局色调颜色非常好。

回答by Ketan Parmar

If you want to set tabbar's tint and barTint colorimplicitly then in your Appdelegate.swift,

如果您想tint and barTint color隐式设置标签栏,则在您的Appdelegate.swift,

    UITabBar.appearance().barTintColor = .orange
    UITabBar.appearance().tintColor = .green

If you want to set tabbar's tint and barTint colorfor specific viewController then in ViewController.swift,

如果你想tint and barTint color为特定的 viewController设置标签栏,然后在ViewController.swift

 self.tabBarController?.tabBar.tintColor = .orange
 self.tabBarController?.tabBar.barTintColor = .green

回答by Pushpa Y

Set tab bar background color with barTintColor:

使用 barTintColor设置标签栏背景颜色:

self.tabBar.barTintColor = UIColor.blueColor()
//or
UITabBar.appearance().barTintColor = UIColor.blueColor()

And for tab bar tint color:

对于标签栏色调颜色:

self.tabBar.tintColor = UIColor.whiteColor() // Selected tab color
//or
UITabBar.appearance().tintColor = UIColor.whiteColor()

enter image description here

在此处输入图片说明

回答by Abhishek Jain

Swift 4+ version

斯威夫特 4+ 版本

UITabBar.appearance().barTintColor = UIColor.red
UITabBar.appearance().tintColor = UIColor.white