UITabBarController:UITabBarItem更新或重新加载它

问题描述 投票:3回答:1

如何更新或重新加载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()

我的T​​abBar是我的rootVC

swift uitabbarcontroller uitabbar uitabbaritem
1个回答
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)
                    }

                }
    }

© www.soinside.com 2019 - 2024. All rights reserved.