iOS 13 黑暗模式適配

1. 顏色適配
2.圖片適配
@available(iOS 13.0, *)
    public init(dynamicProvider: @escaping (UITraitCollection) -> UIColor)
self.view.backgroundColor = UIColor(dynamicProvider: { (coll) -> UIColor in
  if coll.userInterfaceStyle == .dark {
       return UIColor.red
  } else {
        return UIColor.blue
      }
})

又新增一個(gè)UITraitCollection方法,我們可以通過(guò)它來(lái)判斷當(dāng)前系統(tǒng)的模式

open class UITraitCollection : NSObject, NSCopying, NSSecureCoding {...}

UIView UIViewControlelr UIScreen UIWindow都遵從UITraitEnvironment協(xié)議

public protocol UITraitEnvironment : NSObjectProtocol {
    @available(iOS 8.0, *)
    var traitCollection: UITraitCollection { get }
}

UIUserInterfaceStyle是個(gè)枚舉有3種模式

public enum UIUserInterfaceStyle : Int {
    case unspecified
    case light
    case dark
}

在協(xié)議中有一個(gè)traitCollection屬性判斷當(dāng)前處于哪種模式

1.
let style = self.traitCollection.userInterfaceStyle
 switch style {
        case .dark:
            print("黑暗模式")
        case .light:
            print("亮模式")
        case .unspecified:
            print("未指定")
        default:
            break
}
2.
let collection = UITraitCollection.current

如果我們不想適配黑暗模式,全局關(guān)閉黑暗模式在Info.plist文件中,添加key為 User Interface Style,value設(shè)置為Light即可關(guān)閉

關(guān)閉單個(gè)頁(yè)面或試圖的黑暗模式只需設(shè)置overrideUserInterfaceStyle指定模式

self.overrideUserInterfaceStyle = .dark

如果希望一個(gè)子視圖監(jiān)聽(tīng)系統(tǒng)的模式,要將overrideUserInterfaceStyle 設(shè)置成.unspecified

圖片適配


QQ20190921-204412.png
最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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