iOS15導(dǎo)航欄barTintColor設(shè)置無(wú)效及下方黑線無(wú)法隱藏問題

更新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()
        }

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

?著作權(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)容