traitCollectionDidChange strange behavior
Asked Answered
L

1

5
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    super.traitCollectionDidChange(previousTraitCollection)
    if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
        print("changed")
    }
}

The iOS 13 application just goes to background without changing the mode (light/dark) but the "print" function is called. Is it possible to determine the moment when the mode really changed?

Thank You.

Launcher answered 2/12, 2019 at 13:19 Comment(2)
Note it may help to check UIApplication.shared.applicationState == .active to avoid the problem. Though note then you need to make sure you update your interface properly when the app resumes active if the interface style changes while it's not active.Fluorosis
There is an another answer to your question. The reason it is called is the system needs to make snapshots of your app for both light / dark modes, so it displays it in activity apps.Commove
C
2

I observed this myself as well.
The user interface toggles as soon as you go to the background, however - it immediately toggles back.
I guess you should simple follow the changes and react as needed.
You can find the current interface style by checking traitCollection.userInterfaceStyle.
For instance:
let isDark = traitCollection.userInterfaceStyle == .dark

Convincing answered 2/12, 2019 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.