Last active
September 17, 2019 10:43
-
-
Save chrsp/d8a5773aa5fd18083e9b99c7acbc24bd to your computer and use it in GitHub Desktop.
Snippets of code to prepare your app for dark mode (https://developer.apple.com/videos/play/wwdc2019/214)
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
// Only contents of UIKit can use DynamicColors. So if you need the dynamic color behavior | |
// on CALayer, CGColor etc you need to use one of these 3 options: | |
// Option 1 | |
let resolvedColor = UIColor.label.resolvedColor(with: traitCollection) | |
layer.borderColor = resolvedColor.cgColor | |
// Option 2 | |
traitCollection.perfomAsCurrent { | |
layer.borderColor = UIColor.label.cgColor | |
} | |
// Option 3 | |
let savedTraitCollection = UITraitCollection.current | |
UITraitCollection.current = traitCollection | |
layer.borderColor = UIColor.label.cgColor | |
UITraitCollection.current = savedTraitCollection |
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
// Defines a dynamic color that responds to traitCollection.userInterfaceStyle | |
let dynamicColor = UIColor { (traitCollection: UITraitCollection) -> UIColor in | |
if traitCollection.userInterfaceStyle == .dark { | |
return .black | |
} else { | |
return .white | |
} | |
} |
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
// Setting dynamic images manually | |
let image = UIImage(named: "aImage") | |
let asset = image?.imageAsset | |
let resolvedImage = asset?.image(with: traitCollection) |
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
// on iOS 13, UIKit now provides a new property that can be used to change the user interface style | |
// this way, even if the user is using iOS 13 on dark mode we can have screens on light mode or the opposite | |
class UIViewController { | |
var overrideUserInterfaceStyle: UIUserInterfaceStyle | |
} | |
class UIView { | |
var overrideUserInterfaceStyle: UIUserInterfaceStyle | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment