【问题标题】:How to use Light UIUserInterfaceStyle image version in Dark Mode?如何在深色模式下使用 Light UIUserInterfaceStyle 图像版本?
【发布时间】:2020-02-18 18:29:12
【问题描述】:

我用的是图片,这张图片有浅色和深色UIUserInterfaceStyle两个版本,但是当深色模式开启时,我需要在一个UIViewController中使用图片浅色版本。我知道我可以使用tintColor,但我想知道是否有其他方法可以做到这一点。

【问题讨论】:

    标签: ios swift ios13 ios-darkmode uiuserinterfacestyle


    【解决方案1】:

    您可以通过在 viewDidLoad 函数中设置 overrideUserInterfaceStyle 属性来选择退出特定视图控制器的暗模式。示例 sn-p 如下。

    override func viewDidLoad() {
            super.viewDidLoad()
    
            // Always adopt a light interface style.    
            overrideUserInterfaceStyle = .light
        }
    

    要为您的图像视图选择暗模式,您可以进行如下设置。

    imageview. overrideUserInterfaceStyle = .light
    

    您可以通过以下链接进一步了解选择退出暗模式。希望对您有所帮助。

    https://developer.apple.com/documentation/xcode/supporting_dark_mode_in_your_interface/choosing_a_specific_interface_style_for_your_ios_app

    【讨论】:

    • 不,对不起,我指的是图像的轻量版,而不是 UIViewController
    • 我已经更新了答案。您还可以设置 imageview 的 overrideUserInterfaceStyle 以选择退出特定 imageview 的暗模式。
    【解决方案2】:

    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)
    

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 2020-02-18
      • 1970-01-01
      • 2019-12-08
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 1970-01-01
      • 2015-03-22
      相关资源
      最近更新 更多