且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检测iOS 13中的明暗模式更改?

更新时间:2023-12-04 21:15:16

我在 WWDC 2019-会议214 大约在23:30结束.

I found the answer on WWDC 2019 - Session 214 around 23:30.

正如我预期的那样,包括更改颜色时,此函数被调用很多.除了ViewControllerpresentationController的许多其他功能之外.但是有一些专门设计的功能在所有View代表中都有相似的签名.

As I expected, this function is getting called a lot including when colors changing. Along side with many other functions for ViewController and presentationController. But there is some especial function designed for that has a similar signature in all View representers.

看看该会话中的这张图片:

Take a look at this image from that session:

灰色:打电话,但不适合我的问题,绿色:专为此设计

所以我应该调用它并在此函数中检查它:

So I should call it and check it inside this function:

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    super.traitCollectionDidChange(previousTraitCollection)

    if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
        dropShadowIfNeeded()
    }
}

这将确保每次更改仅被调用一次.

This will guarantee to be called just once per change.