![](/img/trans.png)
[英]using xcode 5 interface builder for setting tab bar tint (background) color
[英]UITabBarItem icon not colored correctly for iOS 13 when a bar tint color is specified in Interface Builder in Xcode 11, beta 2
iOS 13 中有一個新的外觀 API。要使用 Xcode 11.0 正確為標簽欄項目的圖標和文本着色,您可以像這樣使用它:
if #available(iOS 13, *) {
let appearance = UITabBarAppearance()
appearance.backgroundColor = .white
appearance.shadowImage = UIImage()
appearance.shadowColor = .white
appearance.stackedLayoutAppearance.normal.iconColor = .black
appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
appearance.stackedLayoutAppearance.normal.badgeBackgroundColor = .blue
appearance.stackedLayoutAppearance.selected.iconColor = .red
appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
self.tabBar.standardAppearance = appearance
}
從表面上看,這似乎是一個錯誤,但是您可以通過在 UITabBar 實例上定義 .unselectedItemTintColor 來緩解它。
self.tabBar.unselectedItemTintColor = [UIColor lightGrayColor];
使用IB中的屬性字段“Image Tint”。
蘋果自己的Podcasts App也有同樣的問題。 當前是一個錯誤。
對於 Objective C,您可以在 AppDelegate 中使用:
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
[[UITabBar appearance] setUnselectedItemTintColor:[UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:0.65f]];
if #available(iOS 13.0, *) {
let appearance = tabBar.standardAppearance
appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.blue]
appearance.stackedLayoutAppearance.normal.iconColor = UIColor.black
appearance.stackedLayoutAppearance.selected.iconColor = UIColor.blue
tabBar.standardAppearance = appearance
} else {
tabBar.unselectedItemTintColor = UIColor.black
tabBar.tintColor = UIColor.blue
}
self.tabBarController?.tabBar.unselectedItemTintColor = UIColor.lightGray
這在 swift 4 中對我有用。只需將它放在override func viewWillDisappear(_ animated: Bool)
方法中,它就會隨着視圖的變化而更新。
所以它看起來像這樣
override func viewWillDisappear(_ animated: Bool) {
self.tabBarController?.tabBar.unselectedItemTintColor = UIColor.lightGray
}
即使這是一個錯誤(我不確定),您也可以使用它來使用您選擇的任何顏色來更改標簽欄項目的顏色
感謝 Samuël 的回答。 這是我的應用程序中的 UITabBar 設置,已經是 2021 年了,但互聯網上關於如何為 iOS 13 及更高版本設置UITabBar
有用信息仍然很少見。
if #available(iOS 13, *) {
let appearance = UITabBarAppearance()
// appearance.backgroundColor = .white
appearance.shadowImage = UIImage()
appearance.shadowColor = .white
appearance.stackedLayoutAppearance.normal.iconColor = .gray
appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.gray]
// appearance.stackedLayoutAppearance.normal.badgeBackgroundColor = .yellow
appearance.stackedLayoutAppearance.selected.iconColor = .systemPink
appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.systemPink]
// set padding between tabbar item title and image
appearance.stackedLayoutAppearance.selected.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 4)
appearance.stackedLayoutAppearance.normal.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 4)
self.tabBar.standardAppearance = appearance
} else {
// set padding between tabbar item title and image
UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 4)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.gray], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.systemPink], for: .selected)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.