ios设置tabbar背景颜色_iOS 13 TabBar的字体颜色及背景颜色的设置
时间: 2023-12-18 08:03:43 浏览: 396
iOS 13中设置TabBar的背景颜色和字体颜色需要使用新的API。以下是设置TabBar背景颜色和字体颜色的示例代码:
```
// 设置TabBar的背景颜色
if #available(iOS 13.0, *) {
let appearance = UITabBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .red // 设置背景颜色
tabBar.standardAppearance = appearance
} else {
tabBar.barTintColor = .red
}
// 设置TabBar的字体颜色
if #available(iOS 13.0, *) {
let appearance = UITabBarAppearance()
appearance.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.green] // 设置正常状态下的字体颜色
appearance.stackedLayoutAppearance.selected.titleTextAttributes = [.foregroundColor: UIColor.blue] // 设置选中状态下的字体颜色
tabBar.standardAppearance = appearance
} else {
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.green], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.blue], for: .selected)
}
```
需要注意的是,iOS 13中可以使用`UITabBarAppearance`来设置TabBar的样式,但在iOS 12及以下系统中需要使用`UITabBarItem.appearance()`来设置。
阅读全文