• How to observe dark mode state in an iOS app • How to react to changes in dark mode state in an iOS app
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
UIKit has had UITraitCollection for a while now. Since iOS 9 you could use UITraitCollection to see whether the device supports 3D Touch (a sad conversation for another day)
In iOS 12, UITraitCollection got a new property:
var userInterfaceStyle: UIUserInterfaceStyle
which supports three cases:light
,dark
, andunspecified
Since UIViewController inherits UITraitEnvironment, you have access to the ViewController's
traitCollection
. This storesuserInterfaceStyle
.UITraitEnviroment also has some nifty protocol stubs that help your code interpret when state changes happen (so when a user switches from the Dark side to the Light side or visa versa). Here's a nice coding example for you:
You can use the following method to check for light, or dark mode in your project:
You can also check for changes in the interface style:
Just like in macOS since Mojave, you can define images for both light and dark mode in your asset catalogue so that those images will be used automatically:
Taken from here.