Possible to reload app when dark/light mode changes?

Viewed 2423

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)?

2 Answers

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

Related