UITabBarController: UITabBarItem Updating it or Reloading it

半世苍凉 提交于 2019-12-21 05:36:08

问题


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


回答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)
                    }

                }
    }



来源:https://stackoverflow.com/questions/58942737/uitabbarcontroller-uitabbaritem-updating-it-or-reloading-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!