3

How do I update or reload the text color on the UITabBarItem?

It will, only if I kill the app and re-open again. Then it will refresh the textColor on the UITabBarItem

Swift 5.1, iOS 12

   func handleRefreshForTabBar() {

        DispatchQueue.main.async {
            //Background
            self.view.backgroundColor = Theme.current.generalBackground

            //Images
            self.tabBar.tintColor = Theme.current.tabTintColor

            //Bar
            self.tabBar.barTintColor = Theme.current.tabBarTintColor
          }
    }


    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        handleRefreshForTabBar()

        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: Theme.current.tabBarSelectedTextColor], for: .selected)
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: Theme.current.tabBarNormalTextColor], for: .normal)
    }

Options that I've tried

tabBar.setNeedsDisplay()
tabBar.setNeedsLayout()

view.reloadInputViews()
view.setNeedDisplay()
view.setNeedsLayout()

My TabBar is my rootVC

Marlhex
  • 1,870
  • 19
  • 27
  • this doesn't answer your question directly but have you looked into this: https://developer.apple.com/documentation/appkit/supporting_dark_mode_in_your_interface? specifically using color set assets for handling light / dark interfaces? – erdos Nov 19 '19 at 22:49
  • Problem is I opt-out from systemColor... so I'm doing it manually, so my app will not detect the iphone appearance that the user has, and I have my color assets but I'm sure that it will not detect the systemColor or colorAssets for any, light, dark. – Marlhex Nov 21 '19 at 00:52

1 Answers1

1
 /// Function that makes the tab bar refresh itself. Specially important for refreshing light or dark mode styles
    func handleRefreshForTabBar() {

                DispatchQueue.main.async {

                    //Set up the same color as the NavBar to avoid bugs
                    self.view.backgroundColor = Theme.current.generalBackground

                    //Images
                    self.tabBar.tintColor = Theme.current.tabTintColor

                    //Bar
                    self.tabBar.barTintColor = Theme.current.tabBarTintColor


                    self.tabBar.items!.forEach { (item) in
                        item.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: Theme.current.tabBarSelectedTextColor], for: .selected)
                        item.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: Theme.current.tabBarNormalTextColor], for: .normal)
                    }

                }
    }

Marlhex
  • 1,870
  • 19
  • 27