My app uses custom color themes but iOS13 users can opt into following dark mode.
I thought I could simply update my colors in the ViewController's
traitCollectionDidChange()
but for some reason, this function is only called the first time the user changes the interface style in the iOS settings.
While this may be sufficient for most users, ideally traitCollectionDidChange()
should be called every time the user changes their iOS settings.
Just very basic:
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
print(#function)
guard traitCollection.userInterfaceStyle != previousTraitCollection?.userInterfaceStyle else { return }
NotificationCenter.default.post(name: NSNotification.Name(Keys.Notifications.updateColorTheme), object: nil)
}
I launch my app, leave it, change appearance in the iOS settings: the next time I open the app (open, not launch), the above function is called and the app updates colors. Now, when I repeat the same process, the function is not called anymore.
When you’re setting up your iPhone or iPad after upgrading to iOS 13 or iPadOS 13, you’ll see a new Appearance screen. Here, you can choose the “Dark” theme to enable the dark mode from the get-go. But you can also switch between the dark and light mode at any time from the Control Center or from the Settings app.
All of Apple’s stock apps and the system UI supports the dark theme. Apple has gone with a true dark mode. This means the background in most places in the UI is pitch black. On iPhones with OLED screens—that’s the iPhone X, XS, and XS Max—this has a two-fold effect. Firstly, the white text looks crisp on the pitch-black screen.
But if you want to stop dark mode from turning off without your consent, here’s how to do that. Start by opening the Settings app on your iPhone or iPad. Next, scroll down, and then tap Display & Brightness. Underneath the Appearance section, turn off the switch next to Automatic.
If a website supports CSS dark mode, Safari will automatically load the dark theme version for you (as you can see in the screenshots below). This feature is enabled by default. If you want to disable it, you can go to Settings > Safari > Advanced > Experimental Features and disable the “Dark Mode CSS Support” feature.
I found that if I set UIViewController's overrideUserInterfaceStyle
property, then the traitCollectionDidChange
method not called, however when I don't set this overrideUserInterfaceStyle
property, the traitCollectionDidChange
is called.
maybe there have some internal method call judge in UIKit based on overrideUserInterfaceStyle
property.
Hope this help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With