Further exploring Dark Mode
In the previous sections, we gave you a lot to think about when either creating or migrating existing apps to Dark Mode under specific circumstances. In this section, we'll take a look at a couple of little "nice to knows" that should always be in the back of your mind when approaching Dark Mode.
Using Dark Mode with SwiftUI
With the announcement of SwiftUI back in June 2019, a massive shift in focus on UI-based development took place. Released at the same time as Dark Mode, and as expected, SwiftUI takes full advantage of switching appearances.
Let's start by taking a look at how we could detect dark mode programmatically in SwiftUI:
- First, we'll create an environment variable that allows us to access the current state of the appearance of the device:
@Environment(\.colorScheme) var appearance
- Next, let's use a simple ternary operator to display some text based on the current appearance:
Text(appearance...