简体   繁体   中英

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.

xcode 图像资产目录外观 These are not my real assets

I'm trying to support dark mode on older iOS versions. 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?

For iOS12 I have tried using the following:

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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