Skip to content

Instantly share code, notes, and snippets.

@artemnovichkov
Last active July 3, 2023 14:51
Show Gist options
  • Save artemnovichkov/9d99660bfd49d0012289c49c77ab18da to your computer and use it in GitHub Desktop.
Save artemnovichkov/9d99660bfd49d0012289c49c77ab18da to your computer and use it in GitHub Desktop.
UIColor convenience initializer for Dark Mode
import UIKit
public extension UIColor {
/// Creates a color object that generates its color data dynamically using the specified colors. For early SDKs creates light color.
/// - Parameters:
/// - light: The color for light mode.
/// - dark: The color for dark mode.
convenience init(light: UIColor, dark: UIColor) {
if #available(iOS 13.0, tvOS 13.0, *) {
self.init { traitCollection in
if traitCollection.userInterfaceStyle == .dark {
return dark
}
return light
}
}
else {
self.init(cgColor: light.cgColor)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment