iOS15导航栏barTintColor设置无效问题
参考链接: https://developer.apple.com/forums/thread/682420
问题描述:
升上iOS15
后,发现使用系统提供的导航栏滑动时会变透明,navigationBar
的barTintColor
设置无效。在有UIScrollView
的情况下,上划后barTintColor
生效,返回时正常。这不坑爹吗这是....
研究了好久,最终在苹果论坛看到了解决方案,泪目T^T
解决方案:
在 iOS 15 中,UIKit 将 的使用扩展scrollEdgeAppearance到所有导航栏,默认情况下会产生透明背景。背景由滚动视图何时滚动导航栏后面的内容来控制。您的屏幕截图表明您已滚动到顶部,因此导航栏在滚动时选择scrollEdgeAppearance了standardAppearance它,并且在以前版本的 iOS 上。
要恢复老样子,你必须采用新UINavigationBar外观的API, UINavigationBarAppearance。删除您现有的自定义并执行如下操作:
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = <your tint color>
navigationBar.standardAppearance = appearance;
navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
在一般情况下,它是最后一行navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance,它通过UINavigationBar为其标准和边缘状态使用相同的外观来解决问题。另请注意,这将导致滚动视图与导航栏重叠 - 我们建议不要设置UINavigationBar.isTranslucent = true.
您还可以将外观代理与上面的代码一起使用,但替换navigationBar.appearance().scrollEdgeAppearance = appearance最后一行(因为您正在构建自己的外观对象 - 其想法是确保 scrollEdge 和标准外观相同)。
代码示例:
class NavigationController: UINavigationController, UINavigationControllerDelegate {
var popDelegate: UIGestureRecognizerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
// 解决侧滑返回失效问题
self.popDelegate = self.interactivePopGestureRecognizer?.delegate
self.delegate = self
self.navigationBar.shadowImage = UIImage()
self.navigationBar.layer.shadowColor = UIColor.hex("c9c9c9").cgColor
self.navigationBar.layer.shadowRadius = 20
self.navigationBar.layer.shadowOpacity = 0.2
self.navigationBar.layer.shadowOffset = CGSize(width: 0, height: 5)
self.navigationBar.tintColor = UIColor.primaryTextColor
self.navigationBar.isTranslucent = false
self.navigationBar.barTintColor = .white
let appearance = UINavigationBar.appearance()
appearance.shadowImage = UIImage()
appearance.layer.shadowColor = UIColor.hex("c9c9c9").cgColor
appearance.layer.shadowRadius = 20
appearance.layer.shadowOpacity = 0.2
appearance.layer.shadowOffset = CGSize(width: 0, height: 5)
appearance.tintColor = UIColor.primaryTextColor //前景色,按钮颜色
appearance.isTranslucent = false // 导航条背景是否透明
appearance.barTintColor = .white //背景色,导航条背景色
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.primaryTextColor, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20, weight: .medium)] // 设置导航条标题颜色,还可以设置其它文字属性,只需要在里面添加对应的属性
// 解决iOS15 barTintColor设置无效的问题,参考https://developer.apple.com/forums/thread/682420
if #available(iOS 15.0, *) {
let newAppearance = UINavigationBarAppearance()
newAppearance.configureWithOpaqueBackground()
newAppearance.backgroundColor = .white
newAppearance.shadowImage = UIImage()
newAppearance.shadowColor = nil
newAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.primaryTextColor, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20, weight: .medium)]
appearance.standardAppearance = newAppearance
appearance.scrollEdgeAppearance = appearance.standardAppearance
}
}
}
总结
以上来自机翻,怎么说呢,UINavigationBarAppearance
和UINavigationBar.appearance()
还是有一些不一样。没有layer, 如果要去掉导航栏默认分割线,需要将appearance.shadowColor = nil
,目前仅在iOS15上使用。如有更好的方法,请多多指教...