让整个 app 在黑暗模式下仍然维持浅色模式的样式
在工程的 info.plist 中增加
NSRequiresAquaSystemAppearance = NO
image.png记得先
右键 show raw keys/values
显示原键值.
监控黑暗模式
func addAppleInterfaceStyleObserver() {
UserDefaults.standard.addObserver(self, forKeyPath: AppleInterfaceStyleKey, options: NSKeyValueObservingOptions.new, context: &AppleInterfaceStyleObservationContext)
}
open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if context == &AppleInterfaceStyleObservationContext {
updateAppTheme()
}
else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
}
}
网友评论