Skip to content

Instantly share code, notes, and snippets.

@chrsp
Last active September 17, 2019 10:43
Show Gist options
  • Save chrsp/d8a5773aa5fd18083e9b99c7acbc24bd to your computer and use it in GitHub Desktop.
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)
// 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
// 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
}
}
// Setting dynamic images manually
let image = UIImage(named: "aImage")
let asset = image?.imageAsset
let resolvedImage = asset?.image(with: traitCollection)
// 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