简体   繁体   English

如何在iOS13之前访问UIImage深色外观

[英]How can I access UIImage dark appearance pre iOS13

I've just added dark mode to my app.我刚刚在我的应用程序中添加了暗模式。

Asset catalogs support multiple appearances for each asset, on iOS 13 this means the correct asset is used when the system is running in dark or light mode.资产目录支持每个资产的多种外观,在 iOS 13 上,这意味着当系统在黑暗或光明模式下运行时使用正确的资产。

xcode 图像资产目录外观 These are not my real assets这些不是我的真实资产

I'm trying to support dark mode on older iOS versions.我正在尝试在较旧的 iOS 版本上支持暗模式。 In my attempts to do so, I have added an override to force dark mode which works for my custom colours and theming but not for the images.在我尝试这样做时,我添加了一个强制暗模式的覆盖,该模式适用于我的自定义颜色和主题,但不适用于图像。

Is it possible to access the dark appearance of an image programatically before iOS13?是否可以在 iOS13 之前以编程方式访问图像的dark外观?

For iOS12 I have tried using the following:对于 iOS12,我尝试使用以下内容:

if #available(iOS 12.0, *) {
    let traits = UITraitCollection(userInterfaceStyle: .dark)
    let image = UIImage(
        named: "Image", 
        in: bundle, 
        compatibleWith: traits
    )
}

This only returns the normal appearance, and the method naming seems to suggest this only checks that the trait collection I've passed is compatible with the image.这只会返回正常的外观,并且方法命名似乎表明这只会检查我传递的特征集合是否与图像兼容。

As far as I know, there's no way to do that with just one asset.据我所知,仅使用一项资产是无法做到这一点的。 Below iOS 13 the system will always take the Any appearance.在 iOS 13 以下,系统将始终采用Any外观。 You would need to create two different image sets with different names and choose either one of them.您需要创建两个具有不同名称的不同图像集,然后选择其中一个。

It is a bit confusing since UIUserInterfaceStyle is available in iOS 12+, but this is likely because macOS got Dark Mode that year.这有点令人困惑,因为UIUserInterfaceStyle在 iOS 12+ 中可用,但这很可能是因为 macOS 在当年获得了暗模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM