ios 更改标签栏的背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36862564/
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 08:57:30  来源:igfitidea点击:

Changing the background color of Tab Bar

iosswiftuitabbarcontrolleruitabbar

提问by MrDank

I am trying to get desired color rendered in the background of Tab Bar however I am facing problems. These are the things that I tried :-

我正在尝试在 Tab Bar 的背景中呈现所需的颜色,但是我遇到了问题。这些是我尝试过的事情:-

  1. Changing the background color of tab bar object from storyboard. The color rendered is always lighter than the desired color.

  2. Programmatically changing the color of the tab bar using the following code inside viewDidLoad()method

        self.tabBar.translucent = false
        self.tabBar.backgroundColor = UIColor(hexString: "323B61")
    

    It doesn't change the color. Instead , the color rendered is white.

  1. 从故事板更改标签栏对象的背景颜色。渲染的颜色总是比所需的颜色浅。

  2. 使用以下代码内viewDidLoad()方法以编程方式更改选项卡栏的颜色

        self.tabBar.translucent = false
        self.tabBar.backgroundColor = UIColor(hexString: "323B61")
    

    它不会改变颜色。相反,呈现的颜色是白色。

How can I get the desired color for Tab Bar?

如何获得 Tab Bar 所需的颜色?

回答by Abhinandan Pratap

To change background colour of UITabBar

更改 UITabBar 的背景颜色

TabBarController* Tcontroller =(TabBarController*)self.window.rootViewController;
Tcontroller.tabBar.barTintColor=[UIColor yourcolour];

Swift 3

斯威夫特 3

Based on the code above, you can get it by doing this

根据上面的代码,你可以通过这样做得到它

let Tcontroller = self.window.rootViewController as? UITabBarController
Tcontroller?.tabBar.barTintColor = UIColor.black // your color

or in more general

或者更一般地说

UITabBar.appearance().barTintColor = UIColor.black // your color

回答by Anurag Sharma

We can also do it from Storyboard

我们也可以从 Storyboard

1) Select the Tab Barfirst:

1)首先选择标签栏

enter image description here

在此处输入图片说明

2) Then from the Attribute Inspector choose Bar Tint colouras shown in the below image:

2) 然后从 Attribute Inspector 中选择Bar Tint color,如下图所示:

enter image description here

在此处输入图片说明

That's it!

就是这样!

回答by Trevor

swift 4

迅捷 4

Inside your UITabBarController

在你的 UITabBarController 里面

tabBar.barTintColor = .purple
tabBar.isTranslucent = false

You also have access to:

您还可以访问:

tabBar.tintColor = .green
tabBar.unselectedItemTintColor = .blue

to change icon colours if you want.

如果需要,可以更改图标颜色。

回答by Madhumitha

try this code

试试这个代码

self.tabBarController.tabBar.barTintColor =  [UIColor colorWithRed:0.376 green:0.729 blue:0.318 alpha:1.000];