Search code examples
iosswiftuitabbarcontrolleruitabbaruitabbarappearance

Change image tint, title tint, background and so on in new UITabbar from iOS 13


I faced a weird problem. I wanted to change the colors of my tabBar in just one of my view controllers of my tabBar. How do I achieve this in iOS > 13?


Solution

  • After searching a lot and finding no working solutions, I succeeded by luck. I found out that you must set an appearance object before I can change them!! But if you set appearance before the override function endAppearanceTransition() of UITabbar it is working, but not anymore. finally, this way worked:

    1. Set an empty appearance object at viewDidLoad of one of the view Controllers or UITabbarViewcontroller to be executed one time:

      override func viewDidLoad() {
          super.viewDidLoad()
          tabBarController?.tabBar.standardAppearance = UITabBarAppearance()
      }
      
    2. Then you can use updating like this at anytime:

      let attrs = [NSAttributedString.Key.foregroundColor: UIColor.green]
      tabBarController?.tabBar.backgroundColor = barColor
      tabBarController?.tabBar.standardAppearance.stackedLayoutAppearance.selected.iconColor = .green
      tabBarController?.tabBar.standardAppearance.stackedLayoutAppearance.normal.iconColor = .green
      tabBarController?.tabBar.standardAppearance.stackedLayoutAppearance.normal.titleTextAttributes = attrs
      tabBarController?.tabBar.standardAppearance.stackedLayoutAppearance.selected.titleTextAttributes = attrs