UITabBarController:UITabBarItem更新或重新加载它

如何更新或重新加载UITabBarItem上的文本颜色?

只有在我终止该应用程序并重新打开后,它才会运行。然后,它将刷新UITabBarItem上的textColor

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

我尝试过的选项

tabBar.setNeedsDisplay()
tabBar.setNeedsLayout()

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

我的TabBar是我的rootVC

mddxfsh 回答:UITabBarController:UITabBarItem更新或重新加载它

 /// 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://www.f2er.com/3070300.html

大家都在问