UIBarAppearance
UIBarAppearance提供两个子类:UITabBarAppearance、UINavigationBarAppearance
一、UITabBarAppearance
func setAppearance() {
tabBar.isTranslucent = false//不透明
tabBar.backgroundColor = UIColor.white
//barTintColor = UIColor.blue //bar背景颜色
tabBar.tintColor = UIColor.yellow//icont颜色
if #available(iOS 13.0, *) {
//系统外观
//let itemAppearance = UITabBarItemAppearance(style: .stacked)
//自定义外观:图标、文字、徽章
let itemAppearance = UITabBarItemAppearance(style: .stacked)
itemAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.purple]
itemAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.red]
itemAppearance.normal.iconColor = UIColor.blue
itemAppearance.selected.iconColor = UIColor.red
itemAppearance.normal.badgeBackgroundColor = UIColor.green
itemAppearance.normal.badgeTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.red]
let appearance = UITabBarAppearance()
/* 没有设置时,系统是会提供默认配置的。
* stackedLayoutAppearance:tabarItem文字、图片垂直排布布局时的外观。一般iPhone竖屏时都是这样的。
* inlineLayoutAppearance: 文字、图片并列排布时的外观。例如iPad的tabar都是并列排布的。
* compactInlineLayoutAppearance: 文字、图片并列排布显示,Size Class为compact环境下的外观。比如 iphone8 tabar在横屏时,文字、图片是并列排布的,size class 为compact width ,compact height。
*/
appearance.stackedLayoutAppearance = itemAppearance
appearance.compactInlineLayoutAppearance = itemAppearance;
appearance.inlineLayoutAppearance = itemAppearance;
tabBar.standardAppearance = appearance;
if #available(iOS 15.0, *) {
//当可滚动视图的内容边缘与tabar对齐时,所呈现的外观。而standardAppearance现在就是正常情况的外观。
//当属性值是nil时,UIKit 将会使用standardAppearance来配置tabar的外观,并将会修改tar的背景为透明背景。
tabBar.scrollEdgeAppearance = appearance;
}
}
}
二、UINavigationBarAppearance
class NavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 13.0, *) {
//不透明
self.navigationBar.isTranslucent = false
let barAppearance = UINavigationBarAppearance()
//标题颜色、字体
barAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
barAppearance.titleTextAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 30)]
// 下横线颜色
barAppearance.shadowColor = UIColor.red
barAppearance.backgroundColor = UIColor.white
// 按钮样式\完成按钮、返回按钮
barAppearance.buttonAppearance = setAppearance(UIColor.red, 25)
barAppearance.doneButtonAppearance = setAppearance(UIColor.purple, 25)
barAppearance.backButtonAppearance = setAppearance(UIColor.yellow, 25)
navigationBar.standardAppearance = barAppearance
navigationBar.scrollEdgeAppearance = barAppearance
//item字体颜色
navigationBar.tintColor = UIColor.red
if #available(iOS 15.0, *) {
navigationBar.compactScrollEdgeAppearance = barAppearance
}
}
}
@available(iOS 13.0, *)
func setAppearance(_ color: UIColor, _ font: CGFloat) -> UIBarButtonItemAppearance {
let itemAppearance = UIBarButtonItemAppearance()
itemAppearance.normal.titleTextAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: font)]
//尝试改变前景颜色,但发现没有发生任何变化。(改用navigationBar.tintColor = UIColor.red)
//itemAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: color]
return itemAppearance
}
}