Reputation: 2043
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.
Upvotes: 8
Views: 8889
Reputation: 1356
The default value of overrideUserInterfaceStyle
is .unspecified
. If you want to be called it again, just revert it to .unspecified
after setting to .light
or `.dark'
Upvotes: 0
Reputation: 2394
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.
Upvotes: 5