#ios #swift #uitabbarcontroller #uitabbar #uitabbaritem
#iOS #swift #uitabbarcontroller #uitabbar #uitabbaritem
Вопрос:
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)
Ответ №1:
Единственное решение, которое я нашел на сегодняшний день, — это снова и снова устанавливать одни и те же элементы TabBarItems каждый раз в viewWillAppear(), что я считаю очень глупым решением, но все еще не могу найти ничего другого, что сработало бы.
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
}