Skip to content

Instantly share code, notes, and snippets.

@bardonadam
Last active August 20, 2021 03:22
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bardonadam/da13690a5cf11e210c50165faa1f3c94 to your computer and use it in GitHub Desktop.
Save bardonadam/da13690a5cf11e210c50165faa1f3c94 to your computer and use it in GitHub Desktop.
DynamicColor property wrapper type to remove boilerplate code when defining dynamic colors to adopt dark mode on iOS 13
@DynamicColor(lightVariant: .black, darkVariant: .white)
static var dynamicLabelColor: UIColor
@propertyWrapper
struct DynamicColor {
let lightVariant: UIColor
let darkVariant: UIColor
var wrappedValue: UIColor {
get {
if #available(iOS 13.0, *) {
return UIColor { (traitCollection: UITraitCollection) -> UIColor in
if traitCollection.userInterfaceStyle == .dark {
return self.darkVariant
}
else {
return self.lightVariant
}
}
}
else {
return self.lightVariant
}
}
}
}
@rickwierenga
Copy link

Awesome 👍

I think it'd be clearer if you return the color style as follows:

if traitCollection.userInterfaceStyle == .dark ? self.darkVariant : self.lightVariant

You can also omit the get statement and just return the value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment