我创建了一个简单的演示,只创建了一个 UITabBarController 的子类并在故事板中设置。
我想在选择时将 TabBarButtonItem 的标题设置为橙色,在正常情况下将标题设置为黑色。以下代码在 iPhone 上的任何 iOS 版本上都可以正常工作,但在 iOS 15 的 iPad(设备和模拟器)上,所选颜色更改为蓝色和有线正常状态颜色。
这是 Apple 的错误还是我错过了什么?(我使用的是 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
如果有人感兴趣,你也可以在 iOS 15、Xcode 13 的 Storyboard 中实现这一点:
现在我们需要配置iPad的内联布局
所以现在我们需要对内联布局做同样的事情,在属性检查器的同一部分中,您将内联属性,将该选项更改为自定义。并按照上述步骤进行设置。
我建议您也对紧凑内联布局执行相同的操作。
如果您使用情节提要而不是对其进行编码,那么您可能还需要考虑配置滚动边缘外观,您将必须重复我们刚刚为滚动边缘外观的标准外观所做的所有操作。
这是ios15的bug,你会在Ipad中发现,只需设置tintColor即可解决这个问题
if #available(iOS 15.0, *) {
tabbar.tintColor = .orange
} else {
tabBar.barTintColor = .orange
}
let appearance = self.tabBar.standardAppearance.copy()
appearance.configureWithTransparentBackground()
appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
let normalAttributes: [NSAttributedString.Key: Any]
= [NSAttributedString.Key.foregroundColor: Constant.AppColor.tabBarItemUnselectedColor]
appearance.stackedLayoutAppearance.normal.titleTextAttributes = normalAttributes
appearance.inlineLayoutAppearance.normal.titleTextAttributes = normalAttributes
appearance.compactInlineLayoutAppearance.normal.titleTextAttributes = normalAttributes
tabBar.standardAppearance = appearance