Skip to content
This repository has been archived by the owner on Jan 14, 2021. It is now read-only.

iOS 13 support for traitCollection.userInterfaceStyle (Dark mode) #90

Open
JorgeFrias opened this issue Nov 25, 2019 · 0 comments
Open

Comments

@JorgeFrias
Copy link

JorgeFrias commented Nov 25, 2019

First of all, awesome extension!

I'm using SwiftGif to display animations along the UI, but with iOS 13 I found some of my animations are not displaying correctly on DarkMode, so I'd like to have trait variations as we have with UIColor:

static var customAccent: UIColor {
    if #available(iOS 13, *) {
        return UIColor { (traitCollection: UITraitCollection) -> UIColor in
            if traitCollection.userInterfaceStyle == .dark {
                return MaterialUI.orange300
            } else {
                return MaterialUI.orange600
            }
        }
    } else {
        return MaterialUI.orange600
    }
}

I can make something similar with UIImage using this code (below), but I cannot get it to work with the Gifs.

extension UIImage {
    /// Creates a dynamic image that supports displaying a different image asset when dark mode is active.
    static func dynamicImageWith(
        light makeLight: @autoclosure () -> UIImage,
        dark makeDark: @autoclosure () -> UIImage
    ) -> UIImage {
        let image = UITraitCollection(userInterfaceStyle: .light).makeImage(makeLight())

        image.imageAsset?.register(makeDark(), with: UITraitCollection(userInterfaceStyle: .dark))

        return image
    }
}

extension UITraitCollection {
    /// Creates the provided image with traits from the receiver.
    func makeImage(_ makeImage: @autoclosure () -> UIImage) -> UIImage {
        var image: UIImage!
        performAsCurrent {
            image = makeImage()
        }
        return image
    }
}

Anyone have any idea on how to solve the problem?

@JorgeFrias JorgeFrias changed the title iOS 13 support for traitCollection.userInterfaceStyle iOS 13 support for traitCollection.userInterfaceStyle (Dark mode) Nov 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

1 participant