Wez
Wez

Reputation: 10712

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 image asset catalog appearances 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.

Upvotes: 3

Views: 3197

Answers (1)

Frank Rupprecht
Frank Rupprecht

Reputation: 10383

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.

Upvotes: 6

Related Questions