iOS15導(dǎo)航欄barTintColor設(shè)置無(wú)效問(wèn)題

iOS15導(dǎo)航欄barTintColor設(shè)置無(wú)效問(wèn)題

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

問(wèn)題描述:

升上iOS15后,發(fā)現(xiàn)使用系統(tǒng)提供的導(dǎo)航欄滑動(dòng)時(shí)會(huì)變透明,navigationBarbarTintColor設(shè)置無(wú)效。在有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)容來(lá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,它通過(guò)UINavigationBar為其標(biāo)準(zhǔn)和邊緣狀態(tài)使用相同的外觀來(lái)解決問(wèn)題。另請(qǐng)注意,這將導(dǎo)致滾動(dòng)視圖與導(dǎo)航欄重疊 - 我們建議不要設(shè)置UINavigationBar.isTranslucent = true.

您還可以將外觀代理與上面的代碼一起使用,但替換navigationBar.appearance().scrollEdgeAppearance = appearance最后一行(因?yàn)槟跇?gòu)建自己的外觀對(duì)象 - 其想法是確保 scrollEdge 和標(biāo)準(zhǔn)外觀相同)。

代碼示例:

class NavigationController: UINavigationController, UINavigationControllerDelegate {

    var popDelegate: UIGestureRecognizerDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = .white

        // 解決側(cè)滑返回失效問(wèn)題
        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è)置其它文字屬性,只需要在里面添加對(duì)應(yīng)的屬性
        
        // 解決iOS15 barTintColor設(shè)置無(wú)效的問(wèn)題,參考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é)

以上來(lái)自機(jī)翻,怎么說(shuō)呢,UINavigationBarAppearanceUINavigationBar.appearance()還是有一些不一樣。沒(méi)有l(wèi)ayer, 如果要去掉導(dǎo)航欄默認(rèn)分割線,需要將appearance.shadowColor = nil,目前僅在iOS15上使用。如有更好的方法,請(qǐng)多多指教...

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容