简体   繁体   中英

UITabBarController resets tab titles color for tabs after presenting new controller

class MainTabBarController: UITabBarController {
   
   override func viewWillAppear(_ animated: Bool) {
       super.viewWillAppear(animated)
       welcomeNavigationController.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.purple], for: .selected)
       createNavigationController.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.systemOrange], for: .selected)
       settingsNavigationController.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.systemGreen], for: .selected)
   }
}

I have different colors for different titles in UITabBarController. The code below works at the beginning.

But after I present the new controller above and go back, the currently selected tab still has its unique color, but all others tabs have default system blue color. And even though viewWillAppear() calling again on coming back to the UITabBarController it doesn't paint titles to different colors again.

So after presenting new controller above the TabBarController it reset all titles color except currently selected and it is impossible to paint them again even with delay. How is it possible, how to fix it?

I don't have in my code any more places where I would change any attributes of UITabBar.

 navigationController.present(newController, animated: true, completion: nil)

....

navigationController.dismiss(animated: true, completion: nil)

The only solution I've found to date is to set the same TabBarItems again and again every time on viewWillAppear() which is I consider very stupid solution but still can't find anything else that would work.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    let welcomeIco = UITabBarItem(title: R.string.localizable.collection(),
                                 image: R.image.collection_dis()?.withRenderingMode(.automatic),
                                 selectedImage: R.image.collection_act()?.withRenderingMode(.alwaysOriginal))
    welcomeIco.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.purple], for: .selected)
    welcomeNavigationController.tabBarItem = welcomeIco
    
    let createIco = UITabBarItem(title: R.string.localizable.create(),
                                 image: R.image.create_dis()?.withRenderingMode(.automatic),
                                 selectedImage: R.image.create_act()?.withRenderingMode(.alwaysOriginal))
    createIco.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.systemOrange], for: .selected)
    createNavigationController.tabBarItem = createIco
    
    let settingsIco = UITabBarItem(title: R.string.localizable.settings(),
                                   image: R.image.settings_dis()?.withRenderingMode(.automatic),
                                 selectedImage: R.image.settings_act()?.withRenderingMode(.alwaysOriginal))
    settingsIco.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.systemGreen], for: .selected)
    settingsNavigationController.tabBarItem = settingsIco
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM