如何在暗模式下使用 Light UIUserInterfaceStyle 图像版本?

Ale*_*tev 3 ios swift ios13 ios-darkmode uiuserinterfacestyle

我使用图像,这个图像有两个版本的 light 和 dark UIUserInterfaceStyle,但是UIViewController当暗模式打开时,我需要在一个版本中使用图像 light 版本。我知道我可以使用tintColor,但我想知道是否有其他方法可以做到这一点。

Fra*_*gel 5

Subramanian 的答案非常适合这个用例。

但是,我真的只需要UIImage一种不同的风格,您可以执行以下操作:

// create a trait collection with `light` interface style
let traitCollection = UITraitCollection(userInterfaceStyle: .light)

// If you have an existing image, you can change it's style like this: 
let lightImage = image.withConfiguration(traitCollection.imageConfiguration)

// Or if you load the image by name, you can do this:
let lightImage = UIImage(named: "imageName", in: nil, compatibleWith: traitCollection)
Run Code Online (Sandbox Code Playgroud)