sinusGob sinusGob - 8 months ago 62
Swift Question

How to change the background color of tab bar programatically?

My goal is simple, change the default background color of tab bar to my own color.

For example, the default looks like this

enter image description here

I created my own subclass of UITabBarController, so that I don't need to change the color on every UIViewController

import UIKit

class MyTabController: UITabBarController {

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBar.backgroundColor = .black
}
}


The result is different from what I expected.

enter image description here

I thought that maybe it's the color, then I changed to a custom UIColor and the color looks exactly the same.

I tried changing the bar tint color as well, but its change the color of the icon when active, not the background

self.tabBar.tintColor = UIColor(red:1.00, green:0.23, blue:0.19, alpha:1.0)


The result will be

enter image description here

What did I do wrong?

Answer Source

You should use self.tabBar.barTintColor or have a look at UIBarStyle and self.tabBar.barStyle and see if that works.