简体   繁体   中英

Changing the background color of Tab Bar

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 :-

  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.

How can I get the desired color for Tab Bar?

To change background colour of UITabBar

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

Swift 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

We can also do it from Storyboard

1) Select the Tab Bar first:

在此处输入图片说明

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

在此处输入图片说明

That's it!

swift 4

Inside your 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.

试试这个代码

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

Swift 4, in viewDidLoad of TabBarController

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

迅捷 5

self.tabBarController.tabBar.backgroundColor = .white

I use this code to configure tab bar in custom subclass of UITabBarController. It supports iOS 15 and XCode 13 updates. Swift 5.

let backgroundColor = UIColor.red
let selectedItemTextColor = UIColor.black
let unselectedItemTextColor = UIColor.white

if #available(iOS 15, *) {
    let tabBarAppearance = UITabBarAppearance()
    tabBarAppearance.backgroundColor = backgroundColor
    tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = [.foregroundColor: selectedItemTextColor]
    tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor: unselectedItemTextColor]
    tabBar.standardAppearance = tabBarAppearance
    tabBar.scrollEdgeAppearance = tabBarAppearance
} else {
    UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: selectedItemTextColor], for: .selected)
    UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: unselectedItemTextColor], for: .normal)
    tabBar.barTintColor = backgroundColor
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM