Reputation: 3681
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
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:
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:
Upvotes: 3
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