iOS15適配以及黑夜模式遇到的問(wèn)題

在iOS開發(fā)中,不得不考慮到黑夜模式的適配,可能不太熟悉的人會(huì)覺得一臉懵,我為了這個(gè)適配專門寫了個(gè)小方法,可能沒有特別高端,希望能幫到你們。

此方法在iOS13的時(shí)候出現(xiàn)了,感覺蘋果是專門為了適配黑夜模式設(shè)計(jì)的。
代碼如下:

extension UIColor {
    class func getColor(_ light:UIColor,_ dark:UIColor) -> UIColor{
      return UIColor { (trait) -> UIColor in
            if trait.userInterfaceStyle == .light {
                return light
            }else{
                return dark
            }
        }
    }
}

iOS15 我遇到了一個(gè)關(guān)于tabbar變成透明的問(wèn)題
此方法可以解決iOS15 tabbar透明的問(wèn)題
可以寫在父類UITabBarController viewDidLoad里面。

代碼如下:

if #available(iOS 15.0, *) {
            
            let appearance = UITabBarAppearance.init()
            UITabBar.appearance().scrollEdgeAppearance = appearance
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = UIColor.white
            UITabBar.appearance().standardAppearance = appearance
            UITabBar.appearance().backgroundColor = UIColor.systemBackground
            UITabBar.appearance().barTintColor = UIColor.white
            UITabBar.appearance().isTranslucent = false
            self.tabBar.tintColor = UIColor.black
            
        }else{
            
            UITabBar.appearance().barTintColor = UIColor.white
            UITabBar.appearance().isTranslucent = false
            self.tabBar.tintColor = UIColor.black
        }

關(guān)于iOS15 如何讓navigationbar變透明
此方法可以寫在 自定義UIViewController父類的viewWillAppear 里面
可通過(guò)if self is HomePageVc(自定義控制器名稱來(lái)判斷哪些頁(yè)面需要透明導(dǎo)航欄)。

代碼如下,僅供參考:

        let dict = [NSAttributedStringKey.foregroundColor:UIColor.getColor(UIColor.black, UIColor.white)]

 if #available(iOS 15, *) {
                
                let barApp = UINavigationBarAppearance()
                barApp.backgroundColor = UIColor.clear
                if self is HomePageVc(此處可判斷哪些地方需要改變title顏色) {
                    
                    barApp.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.getColor(UIColor.black, UIColor.black)]
                    
                }else{
                    
                    barApp.titleTextAttributes = dict
                    
                }
                
                barApp.backgroundEffect = nil
                barApp.shadowColor = nil
                self.navigationController?.navigationBar.scrollEdgeAppearance = nil
                self.navigationController?.navigationBar.standardAppearance = barApp
                
            }else{
                
                self.navigationController?.navigationBar.titleTextAttributes = dict
                self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)  
                self.navigationController?.navigationBar.shadowImage = UIImage()
                
            }
            
            self.navigationController?.navigationBar.isTranslucent = true

將navigationbar變非透明,以及iOS15 解決導(dǎo)航欄不隨黑夜模式發(fā)生變化的代碼

            if #available(iOS 15, *) {
                
                let barApp = UINavigationBarAppearance()
                barApp.backgroundColor = UIColor.getColor(UIColor.white, UIColor.black)
                barApp.shadowColor = UIColor.white
                barApp.titleTextAttributes = dict
                self.navigationController?.navigationBar.scrollEdgeAppearance = barApp
                self.navigationController?.navigationBar.standardAppearance = barApp
                
                
            }else{
                
                self.navigationController?.navigationBar.barTintColor = UIColor.getColor(UIColor.white, UIColor.black)
                self.navigationController?.navigationBar.titleTextAttributes = dict
                self.navigationController?.navigationBar.shadowImage = UIImage.init()
                self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
                
            }
            self.navigationController?.navigationBar.isTranslucent = false

筆者也是為了培養(yǎng)自己的習(xí)慣,文筆不好,記錄一下自己遇到的問(wèn)題,如有不對(duì)請(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)容

  • 本文作為自己準(zhǔn)備適配iOS15所用,在開始適配之前,先去學(xué)習(xí)各位同學(xué)的文章,記錄在此備用。 1、導(dǎo)航欄UINavi...
    iOS_zy閱讀 14,736評(píng)論 5 61
  • 本文主要分享一下 iOS15 上適配方案,僅做開發(fā)記錄使用,開發(fā)過(guò)程中通過(guò)使用陸續(xù)增加。 iOS15 的適配,很重...
    smile麗語(yǔ)閱讀 5,396評(píng)論 11 24
  • iOS15導(dǎo)航欄barTintColor設(shè)置無(wú)效問(wèn)題 參考鏈接: https://developer.apple....
    霸哥終結(jié)者閱讀 10,117評(píng)論 5 12
  • 以iOS15和xcode13為環(huán)境基礎(chǔ),iOS15適配的一些更改和調(diào)整。 UINavigationBar UITa...
    剛剛下課閱讀 1,018評(píng)論 0 0
  • 在開發(fā)中經(jīng)常會(huì)遇到一些問(wèn)題,剛開始感覺匪夷所思,但是當(dāng)你真正探究,理解了就會(huì)發(fā)現(xiàn)確實(shí)如此。 19、translat...
    魔性佛心閱讀 1,471評(píng)論 1 4

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