繁体   English   中英

UITabBarController 在呈现新的 controller 后重置选项卡的选项卡标题颜色

[英]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)
   }
}

对于 UITabBarController 中的不同标题,我有不同的 colors。 下面的代码在开始时有效。

但是在我展示了上面的新 controller 和 go 之后,当前选择的选项卡仍然具有其独特的颜色,但所有其他选项卡都具有默认的系统蓝色。 即使viewWillAppear()在返回 UITabBarController 时再次调用它,它也不会再次将标题绘制到不同的 colors 上。

因此,在 TabBarController 上方呈现新的 controller 后,它会重置除当前选定的所有标题颜色,并且即使延迟也无法再次绘制它们。 怎么可能,如何解决?

我的代码中没有更多地方可以更改 UITabBar 的任何属性。

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

……

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

迄今为止我发现的唯一解决方案是每次在 viewWillAppear() 上一次又一次地设置相同的 TabBarItems,我认为这是非常愚蠢的解决方案,但仍然找不到其他可行的方法。

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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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