“traitCollectionDidChange”在 iOS 17.0 中已弃用。我如何使用替换品?

Adh*_*ouf 1 user-interface uikit ios swift uitraitcollection

我正在尝试使用此功能,但它不适用于 iOS 17。\n我想每次在深色和浅色模式之间切换时进行更改。

\n

这是我的功能:

\n
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {\n    super.traitCollectionDidChange(previousTraitCollection)\n\n    // Check if the user interface style has changed\n    if self.traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {\n        // User interface style has changed (light to dark or vice versa)\n        if self.traitCollection.userInterfaceStyle == .light {\n            // Code to execute in light mode\n            print("App switched to light mode")\n        } else {\n            // Code to execute in dark mode\n            print("App switched to dark mode")\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

这就是问题所在:

\n
\n

\xe2\x80\x98traitCollectionDidChange\xe2\x80\x99 在 iOS 17.0 中已弃用:使用 UITraitChangeObservable 协议中声明的特征更改注册 API

\n
\n

我应该使用registerForTraitChanges(_:handler:)还是registerForTraitChanges(_:target:action:)代替?我不知道如何使用它。

\n

Han*_*ash 6

traitCollectionDidChange自 iOS 17.0 起已弃用。该解决方案取决于您的应用程序的部署目标。如果您的应用程序的部署目标早于 iOS 17.0(Xcode 15 支持回至 iOS 12),那么继续使用traitCollectionDidChange. 即使在装有 iOS 17 的设备上,它也会继续被调用。如果您有较旧的部署目标,您甚至不会收到弃用警告。

If your app's deployment target is iOS 17.0 or later then you should not override traitCollectionDidChange since it is now deprecated. As the documentation for traitCollectionDidChange shows, you instead make use of either registerForTraitChanges(_:handler:) or registerForTraitChanges(_:target:action:).

The documentation for both of the new methods provide examples of how to use them. The key is to find the trait(s) your code wishes to work with.

In your case you wish to handle a change to UITraitUserInterfaceStyle. You can find the list of traits that you can register for on the page for UIMutableTraits.

Update your code by removing the use of traitCollectionDidChange and adding the following to viewDidLoad:

registerForTraitChanges([UITraitUserInterfaceStyle.self], handler: { (self: Self, previousTraitCollection: UITraitCollection) in
    if self.traitCollection.userInterfaceStyle == .light {
        // Code to execute in light mode
        print("App switched to light mode")
    } else {
        // Code to execute in dark mode
        print("App switched to dark mode")
    }
})
Run Code Online (Sandbox Code Playgroud)

Note that the documentation clearly states that you can safely ignore the return value and you do not need to unregister. You would want to make use of the return value and unregister if you had a case where your registration has a more limited lifetime.

Your update doesn't need to call hasDifferentColorAppearance(comparedTo:) since the handler code is only going to be called when the user interface style changes in this case.


Note - if your deployment target is older than iOS 17, do not use both the old and new code since both sets of code will actually be called when run on devices with iOS 17. You would end up having two similar sets of code each wrapped with appropriate if #available checks.