John Doe
John Doe

Reputation: 3681

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.

Upvotes: 3

Views: 1087

Answers (2)

Joe
Joe

Reputation: 8986

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

Upvotes: 3

matt
matt

Reputation: 535556

I can do this with objective-c by using [[UITabBar appearance] setBarTintColor:[UIColor blackColor]];

Well, the Swift equivalent of that is UITabBar.appearance().barTintColor = UIColor.black. (I don't know why you code all refers to white if what you want is black.)

The fact that you are speaking Swift rather than Objective-C makes no difference to this functionality. If this works in an Objective-C version of your app but not in a Swift version, then you are doing something else different in the Swift version — something you have not revealed to us.

Upvotes: 0

Related Questions