iosswiftios13ios-darkmodeuiuserinterfacestyle

How to use Light UIUserInterfaceStyle image version in Dark Mode?


I use image, this image has two versions for light and dark UIUserInterfaceStyle, but I need to use the image light version in one UIViewController when Dark Mode is on. I know I can use tintColor, but I want to find out if other ways to do this.


Solution

  • Subramanian's answer is perfect for this use case.

    However, I you really need just the UIImage in a different style, you can do the following:

    // 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)