问题一: NavigationBar背景颜色失效,背景色默认透明,滑动时显示
// 在AppDelegate 全局设置导航栏
if #available(iOS 15.0, *) {
let navAppear = UINavigationBarAppearance()
//使用默认背景和阴影值配置条形外观对象
navAppear.configureWithOpaqueBackground()
//导航条背景色
navAppear.backgroundColor = UIColor.white
//这儿可以设置shadowColor透明或设置shadowImage为透明图片去掉导航栏的黑线
navAppear.shadowColor = UIColor.clear
navAppear.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white,NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 17)]
// 不使用毛玻璃效果
appearance.backgroundEffect = nil;
UINavigationBar.appearance().scrollEdgeAppearance = navAppear
UINavigationBar.appearance().standardAppearance = navAppear
}
问题二: 在特定控制器中设置导航栏透明不成功
// 首先我们要先设置导航栏背景图片为透明色
self.navigationController?.navigationBar.setBackgroundImage(UIImage.imageWith(color: .clear), for: UIBarMetrics.default)//设置导航栏背景图,此处用颜色替代
// 其次再适配 iOS15
if #available(iOS 15.0, *) {
let navAppear = UINavigationBarAppearance()
//使用默认背景和阴影值配置条形外观对象
navAppear.configureWithOpaqueBackground()
//导航条背景色
navAppear.backgroundColor = UIColor.clear
//这儿可以设置shadowColor透明或设置shadowImage为透明图片去掉导航栏的黑线
navAppear.shadowColor = UIColor.clear
navAppear.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white,NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 17)]
// 不使用毛玻璃效果
appearance.backgroundEffect = nil;
// 切记此时需要设置当前控制器的 navigationController
self.navigationController?.navigationBar.scrollEdgeAppearance = navAppear
self.navigationController?.navigationBar.standardAppearance = navAppear
}