我创建了一个简单的演示,只创建了一个 UITabBarController 的子类并在故事板中设置。
我想在选择时将 TabBarButtonItem 的标题设置为橙色,在正常情况下将标题设置为黑色。以下代码在 iPhone 上的任何 iOS 版本上都可以正常工作,但在 iOS 15 的 iPad(设备和模拟器)上,所选颜色更改为蓝色和有线正常状态颜色。
这是苹果的错误还是我错过了什么?(我使用的是 Xcode13)
class CustomViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let tabBarAppearnace = UITabBarAppearance()
let tabFont = UIFont.boldSystemFont(ofSize: 18)
let selectedAttributes: [NSAttributedString.Key: Any]
= [NSAttributedString.Key.font: tabFont, NSAttributedString.Key.foregroundColor: UIColor.orange]
let normalAttributes: [NSAttributedString.Key: Any]
= [NSAttributedString.Key.font: tabFont, NSAttributedString.Key.foregroundColor: UIColor.black]
tabBarAppearnace.stackedLayoutAppearance.normal.titleTextAttributes = normalAttributes
tabBarAppearnace.stackedLayoutAppearance.selected.titleTextAttributes = selectedAttributes
tabBar.standardAppearance = tabBarAppearnace
}
}
对于 iPadOS,您必须使用inlineLayoutAppearance
属性,因为在 iPad 上,TabBar 中的项目默认情况下内联显示(标题和图标彼此相邻显示)。
但实际上你还应该配置compactInlineLayoutAppearance
否则,如果您在 iPhone 上使用横向模式,则您的自定义样式将不适用。
class CustomViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let tabBarAppearnace = UITabBarAppearance()
let tabFont = UIFont.boldSystemFont(ofSize: 18)
let selectedAttributes: [NSAttributedString.Key: Any]
= [NSAttributedString.Key.font: tabFont, NSAttributedString.Key.foregroundColor: UIColor.orange]
let normalAttributes: [NSAttributedString.Key: Any]
= [NSAttributedString.Key.font: tabFont, NSAttributedString.Key.foregroundColor: UIColor.black]
tabBarAppearnace.stackedLayoutAppearance.normal.titleTextAttributes = normalAttributes
tabBarAppearnace.stackedLayoutAppearance.selected.titleTextAttributes = selectedAttributes
//New
tabBarAppearnace.inlineLayoutAppearance.normal.titleTextAttributes = normalAttributes
tabBarAppearnace.inlineLayoutAppearance.selected.titleTextAttributes = selectedAttributes
tabBarAppearnace.compactInlineLayoutAppearance.normal.titleTextAttributes = normalAttributes
tabBarAppearnace.compactInlineLayoutAppearance.selected.titleTextAttributes = selectedAttributes
tabBar.standardAppearance = tabBarAppearnace
}
}
欲了解更多信息:https://developer.apple.com/documentation/uikit/uitabbarappearance https://developer.apple.com/documentation/uikit/uitabbarappearance
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)