[英]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.