iOS15導(dǎo)航欄barTintColor設(shè)置無效問題
參考鏈接: https://developer.apple.com/forums/thread/682420
問題描述:
升上iOS15
后,發(fā)現(xiàn)使用系統(tǒng)提供的導(dǎo)航欄滑動(dòng)時(shí)會(huì)變透明,navigationBar
的barTintColor
設(shè)置無效忌堂。在有UIScrollView
的情況下辨绊,上劃后barTintColor
生效,返回時(shí)正常圈驼。這不坑爹嗎這是....
研究了好久妆绞,最終在蘋果論壇看到了解決方案,淚目T^T
解決方案:
在 iOS 15 中橄镜,UIKit 將 的使用擴(kuò)展scrollEdgeAppearance到所有導(dǎo)航欄,默認(rèn)情況下會(huì)產(chǎn)生透明背景冯乘。背景由滾動(dòng)視圖何時(shí)滾動(dòng)導(dǎo)航欄后面的內(nèi)容來控制洽胶。您的屏幕截圖表明您已滾動(dòng)到頂部,因此導(dǎo)航欄在滾動(dòng)時(shí)選擇scrollEdgeAppearance了standardAppearance它裆馒,并且在以前版本的 iOS 上姊氓。
要恢復(fù)老樣子丐怯,你必須采用新UINavigationBar外觀的API, UINavigationBarAppearance翔横。刪除您現(xiàn)有的自定義并執(zhí)行如下操作:
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = <your tint color>
navigationBar.standardAppearance = appearance;
navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
在一般情況下读跷,它是最后一行navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance,它通過UINavigationBar為其標(biāo)準(zhǔn)和邊緣狀態(tài)使用相同的外觀來解決問題禾唁。另請注意效览,這將導(dǎo)致滾動(dòng)視圖與導(dǎo)航欄重疊 - 我們建議不要設(shè)置UINavigationBar.isTranslucent = true.
您還可以將外觀代理與上面的代碼一起使用,但替換navigationBar.appearance().scrollEdgeAppearance = appearance最后一行(因?yàn)槟跇?gòu)建自己的外觀對象 - 其想法是確保 scrollEdge 和標(biāo)準(zhǔn)外觀相同)荡短。
代碼示例:
class NavigationController: UINavigationController, UINavigationControllerDelegate {
var popDelegate: UIGestureRecognizerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
// 解決側(cè)滑返回失效問題
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 // 導(dǎo)航條背景是否透明
appearance.barTintColor = .white //背景色,導(dǎo)航條背景色
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.primaryTextColor, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20, weight: .medium)] // 設(shè)置導(dǎo)航條標(biāo)題顏色肢预,還可以設(shè)置其它文字屬性矛洞,只需要在里面添加對應(yīng)的屬性
// 解決iOS15 barTintColor設(shè)置無效的問題,參考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
}
}
}
總結(jié)
以上來自機(jī)翻烫映,怎么說呢沼本,UINavigationBarAppearance
和UINavigationBar.appearance()
還是有一些不一樣。沒有l(wèi)ayer, 如果要去掉導(dǎo)航欄默認(rèn)分割線锭沟,需要將appearance.shadowColor = nil
抽兆,目前僅在iOS15上使用。如有更好的方法族淮,請多多指教...