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


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中有不同标题的不同颜色。下面的代码一开始就有效。

但在我展示了上面的新控制器并返回后,当前选择的选项卡仍然有其唯一的颜色,但所有其他选项卡都有默认的系统蓝色。即使viewWillAppear((在返回UITabBarController时再次调用,它也不会再次将标题绘制为不同的颜色。

因此,在TabBarController上方显示新控制器后,它会重置除当前选定之外的所有标题颜色,即使有延迟,也不可能再次绘制它们这怎么可能,怎么解决?

我的代码中没有任何地方可以更改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
}

最新更新