Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 13 Dark mode: traitCollectionDidChange only called the first time

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.

like image 366
nontomatic Avatar asked Oct 25 '19 11:10

nontomatic


People also ask

How to enable Dark mode in iOS 13?

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.

What is the dark mode on the iPhone XS Max?

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.

How to stop Dark mode from turning off without consent?

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.

How to enable CSS dark mode in Safari?

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.


1 Answers

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.

like image 173
frank Avatar answered Jan 02 '23 11:01

frank