Last active
July 3, 2023 14:51
-
-
Save artemnovichkov/9d99660bfd49d0012289c49c77ab18da to your computer and use it in GitHub Desktop.
UIColor convenience initializer for Dark Mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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