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

Ios 如何在暗模式下使用Light UIUserInterfaceStyle图像版本?,ios,swift,ios13,ios-darkmode,uiuserinterfacestyle,Ios,Swift,Ios13,Ios Darkmode,Uiuserinterfacestyle,我使用图像,此图像有两个版本用于明暗UIUserInterfaceStyle,但当暗模式打开时,我需要在一个UIViewController中使用图像明暗版本。我知道我可以使用tintColor,但我想知道是否有其他方法可以做到这一点。通过在viewDidLoad函数中设置overrideUserInterfaceStyle属性,可以选择退出特定视图控制器的暗模式。示例代码段如下所示 override func viewDidLoad() { super.viewDidLoad(

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

通过在viewDidLoad函数中设置overrideUserInterfaceStyle属性,可以选择退出特定视图控制器的暗模式。示例代码段如下所示

override func viewDidLoad() {
        super.viewDidLoad()

        // Always adopt a light interface style.    
        overrideUserInterfaceStyle = .light
    }
imageview. overrideUserInterfaceStyle = .light
要选择图像视图的暗模式,可以按如下设置

override func viewDidLoad() {
        super.viewDidLoad()

        // Always adopt a light interface style.    
        overrideUserInterfaceStyle = .light
    }
imageview. overrideUserInterfaceStyle = .light
您可以从下面的链接进一步查看关于选择退出黑暗模式的信息。希望能有帮助


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)

不,对不起,我指的是图像的浅色版本,不是UIViewControllerI。我已经更新了答案。还可以将imageview的overrideUserInterfaceStyle设置为退出特定imageview的暗模式。