John Doe John Doe - 19 days ago 16
Objective-C Question

Swift - Can't update UITabBar barTintColor

in

viewDidLoad
I've tried the following to update the background or "barTintColor" of my UITabBar. I can do this with objective-c by using
[[UITabBar appearance] setBarTintColor:[UIColor blackColor]];
.

What I've tried:

UITabBar.appearance().barTintColor = UIColor.white

self.navigationController?.navigationBar.barTintColor = UIColor.white;

UINavigationBar.appearance().barTintColor = UIColor.white

UINavigationBar.appearance().backgroundColor = UIColor.white


I'd like to avoid having some weird NSNotificationCenter solution and updating from AppDelegate. Why is this so difficult to achieve in Swift? Would appreciate any insight.

Joe Joe
Answer

Note: Below methods should work in Swift 3.Below code has to go inside your viewDidLoad to make it to work.

Method 1:

    tabBarController?.tabBar.barTintColor = UIColor.white
    tabBarController?.tabBar.tintColor = UIColor.red
    tabBarController?.tabBar.isTranslucent = false

Output:

enter image description here

Method 2:

Note: I am setting barTintColour to green to show both method works..

    UITabBar.appearance().barTintColor = UIColor.green // Its strange why this method didn't worked for you?.Try updating your post with viewDidLoad.Its a better way to understand the issue.
    UITabBar.appearance().tintColor = UIColor.purple

Output:

enter image description here

Comments