简体   繁体   中英

Possible to reload app when dark/light mode changes?

My app makes extensive use of CAGradientLayers which apparently do not automatically update their colors when dark/light mode is switched. They will update their colors when the app is closed and reopened though.

Im aware i can use the following code to detect the change and reload each view individually

 override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        // 
    }

However I am looking for a fix that does not require me to update each view individually. Is there a way to detect a light/dark mode change (possibly within the app delegate) and force the entire app to reload itself (as if it had been force closed and reopened)?

There is no way to "reload your app" in this way. The issue arises because CAGradientLayer uses CGColor , which doesn't dynamically adapt to the system style.

I resolved the issue by wrapping a UIView around the gradient layer that implements traitCollectionDidChange to update the gradient's colors accordingly. Maybe you can do the same and use this as subview instead of a sublayer.

You could create a super class UIViewController that override traitCollectionDidChange

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM