更新xcode13以后,編譯工程,導(dǎo)航欄的問題比較明顯。主頁(yè)問題是UINavigationBar部分屬性的設(shè)置在iOS15上是無(wú)效的
舊的代碼運(yùn)行成了這樣的效果

截屏2021-11-01 下午4.35.49.png
下面是之前的的效果圖

截屏2021-11-01 下午4.19.03.png
最終在蘋果論壇看到了解決方案
在 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。下面是兼容老版本寫法
let navigationBar = self.navigationController?.navigationBar
if #available(iOS 15, *) {
let app = UINavigationBarAppearance.init()
app.configureWithOpaqueBackground() // 重置背景和陰影顏色
app.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize),
NSAttributedString.Key.foregroundColor: titleColor
]
app.backgroundColor = color // 設(shè)置導(dǎo)航欄背景色
app.shadowImage = imageWithColor(.clear) // 設(shè)置導(dǎo)航欄下邊界分割線透明
navigationBar?.scrollEdgeAppearance = app // 帶scroll滑動(dòng)的頁(yè)面
navigationBar?.standardAppearance = app // 常規(guī)頁(yè)面
}else{
// 設(shè)置導(dǎo)航欄背景色
navigationBar?.barTintColor = color
// 設(shè)置導(dǎo)航條上的標(biāo)題
navigationBar?.titleTextAttributes = [NSAttributedString.Key.foregroundColor:titleColor]
// 取消半透明效果
navigationBar?.isTranslucent = false
// 設(shè)置導(dǎo)航欄返回按鈕的顏色
navigationBar?.tintColor = UIColor.black
let naviItem = UIBarButtonItem.appearance()
naviItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.black,NSAttributedString.Key.font:UIFont.systemFont(ofSize: fontSize)], for: UIControl.State())
// 設(shè)置導(dǎo)航欄下邊界分割線透明
navigationBar?.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navigationBar?.shadowImage = UIImage()
}