iOS15導航欄barTintColor設置無效問題

iOS15導航欄barTintColor設置無效問題

參考鏈接: https://developer.apple.com/forums/thread/682420

問題描述:

升上iOS15后,發現使用系統提供的導航欄滑動時會變透明,navigationBarbarTintColor設置無效。在有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
        }
    }

}

總結

以上來自機翻,怎么說呢,UINavigationBarAppearanceUINavigationBar.appearance()還是有一些不一樣。沒有layer, 如果要去掉導航欄默認分割線,需要將appearance.shadowColor = nil,目前僅在iOS15上使用。如有更好的方法,請多多指教...

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容