更改标签栏的背景颜色

Changing the background color of Tab Bar

我正在尝试在选项卡栏的背景中呈现所需的颜色,但我遇到了问题。 这些是我尝试过的东西:-

  1. 正在更改故事板中标签栏对象的背景颜色。呈现的颜色总是比想要的颜色浅。

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

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

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

如何获得所需的标签栏颜色?

更改 UITabBar 的背景颜色

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

Swift 3

根据上面的代码,这样做就可以得到

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

或者更笼统地说

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

试试这个代码

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

我们也可以从 Storyboard

1) Select Tab Bar 第一:

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

就是这样!

swift 4

在你的 UITabBarController 中

tabBar.barTintColor = .purple
tabBar.isTranslucent = false

您还可以访问:

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

根据需要更改图标颜色。

swift 5

self.tabBarController.tabBar.backgroundColor = .white

Swift 4、在TabBarController的viewDidLoad中

    self.tabBar.tintColor = UIColor.white // tab bar icon tint color
    self.tabBar.isTranslucent = false
    UITabBar.appearance().barTintColor = UIColor.blue // tab bar background color