Last active
August 20, 2021 03:22
-
-
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
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
@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 | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.