Skip to content

Instantly share code, notes, and snippets.

@nurtugan
Created March 8, 2020 14:52
Show Gist options
  • Save nurtugan/6ba91683352a827d73cae35e1b050b5c to your computer and use it in GitHub Desktop.
Save nurtugan/6ba91683352a827d73cae35e1b050b5c to your computer and use it in GitHub Desktop.
Dark mode color adapter
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