swiftgif/SwiftGif

iOS 13 support for traitCollection.userInterfaceStyle (Dark mode)

Opened this issue · 0 comments

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?