[英]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.我的应用广泛使用 CAGradientLayers,当切换暗/亮模式时,它们显然不会自动更新其 colors。 They will update their colors when the app is closed and reopened though.
当应用程序关闭并重新打开时,他们将更新他们的 colors。
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.出现问题是因为
CAGradientLayer
使用CGColor
,它不能动态适应系统样式。
I resolved the issue by wrapping a UIView
around the gradient layer that implements traitCollectionDidChange
to update the gradient's colors accordingly.我通过在实现
traitCollectionDidChange
的渐变层周围包裹UIView
来解决该问题,以相应地更新渐变的 colors。 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
您可以创建一个覆盖 traitCollectionDidChange 的超级 class
traitCollectionDidChange
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.