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