Created
March 8, 2020 14:52
-
-
Save nurtugan/6ba91683352a827d73cae35e1b050b5c to your computer and use it in GitHub Desktop.
Dark mode color adapter
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
extension UIColor { | |
static func adapt(lightModeColor: UIColor?, darkModeColor: UIColor?) -> UIColor { | |
guard let lightModeColor = lightModeColor, | |
let darkModeColor = darkModeColor else { | |
fatalError("No color") | |
} | |
if #available(iOS 13, *) { | |
return UIColor { (traitCollection: UITraitCollection) -> UIColor in | |
if traitCollection.userInterfaceStyle == .dark { | |
/// Return the color for Dark Mode | |
return darkModeColor | |
} else { | |
/// Return the color for Light Mode | |
return lightModeColor | |
} | |
} | |
} else { | |
/// Return a fallback color for iOS 12 and lower. | |
return lightModeColor | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment