I’m creating a UITabBarController programmatically with a few tabs, but for some reason I can not get the normal state of the tabs icons or text to change color. Its defaulting to a system white. I can only modify the selected state.
I’ve tried creating the tabs in multiple ways, but all seem to return the same result. The only thing I’ve managed to work around is by forcing the icons (which are templates) to render as originals after tinting them. This only works for the icons, leaving the text in the system white color, so i’ve disabled it as it looks worse to have one of each
private func configureTabs() {
let inbox = InboxViewController()
inbox.tabBarItem = UITabBarItem(
title: "INBOX",
//image: UIImage(named: "tab-inbox")?.withTintColor(AppColors.t3, renderingMode: .alwaysOriginal),
//selectedImage: UIImage(named: "tab-inbox")?.withTintColor(AppColors.t1, renderingMode: .alwaysOriginal)
image: UIImage(named: "tab-inbox"),
selectedImage: UIImage(named: "tab-inbox")
)
let capture = CaptureViewController()
capture.tabBarItem = UITabBarItem(
title: "CAPTURE",
//image: UIImage(named: "tab-inbox")?.withTintColor(AppColors.t3, renderingMode: .alwaysOriginal),
//selectedImage: UIImage(named: "tab-inbox")?.withTintColor(AppColors.t1, renderingMode: .alwaysOriginal)
image: UIImage(named: "tab-capture"),
selectedImage: UIImage(named: "tab-capture")
)
tabBar.tintColor = AppColors.t1
tabBar.unselectedItemTintColor = AppColors.t3
viewControllers = [
inbox,
capture
]
}
I’ve also tried setting the Appearance proxy in 2 different ways, with many variations of settings in each:
private func configureAppearance() {
// NavBar
let navAppearance = UINavigationBarAppearance()
navAppearance.configureWithDefaultBackground()
navAppearance.backgroundColor = AppColors.bg
navAppearance.shadowColor = AppColors.border
navigationController?.navigationBar.standardAppearance = navAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navAppearance
navigationController?.navigationBar.tintColor = AppColors.t1
// TabBar
let normalColor = AppColors.t3
let selectedColor = AppColors.t1
let normalFont = UIFont(name: UIFont.JetBrainsMono.regular, size: 10) as Any
let selectedFont = UIFont(name: UIFont.JetBrainsMono.regular_semiBold, size: 10) as Any
let normalTitleAttributes: [NSAttributedString.Key: Any] = [
.font: normalFont,
.foregroundColor: normalColor
]
let selectedTitleAttributes: [NSAttributedString.Key: Any] = [
.font: selectedFont,
.foregroundColor: selectedColor
]
let appearance = UITabBarAppearance()
//appearance.configureWithOpaqueBackground()
func configure(_ itemAppearance: UITabBarItemAppearance) {
itemAppearance.normal.iconColor = normalColor
itemAppearance.normal.titleTextAttributes = normalTitleAttributes
itemAppearance.selected.iconColor = selectedColor
itemAppearance.selected.titleTextAttributes = selectedTitleAttributes
}
configure(appearance.stackedLayoutAppearance)
configure(appearance.inlineLayoutAppearance)
configure(appearance.compactInlineLayoutAppearance)
tabBar.standardAppearance = appearance
tabBar.scrollEdgeAppearance = appearance
tabBar.tintColor = selectedColor
tabBar.unselectedItemTintColor = normalColor
}
and with a constraint:
func setupAppearence() {
let appearance = UITabBarItem.appearance(whenContainedInInstancesOf: [MainTabBarController.self])
appearance.setTitleTextAttributes([
NSAttributedString.Key.foregroundColor: AppColors.t3,
//NSAttributedString.Key.font: UIFont.custom(ofType: .medium, andSize: 10)
], for: .normal)
appearance.setTitleTextAttributes([
NSAttributedString.Key.foregroundColor: AppColors.t1,
//NSAttributedString.Key.font: UIFont.custom(ofType: .medium, andSize: 10)
], for: .selected)
self.tabBar.shadowImage = nil
self.tabBar.tintColor = AppColors.t1
self.tabBar.unselectedItemTintColor = AppColors.t3
}
I can get the font family, size and weight to change, but not the color. I thought maybe my AppColors.t1 might have been incorrectly set somewhere, so I tweaked that value and only the selected state changed. So its not using my whiteish .t1 color as the normal state, its picking up a system color from somewhere, but I cant figure out where.
Have I missed a property somewhere? is there a liquid glass mode I have to toggle on to allow different colors or something? What have I missed? AI has been no help
