Updated 30 August 2019
iOS 13 came with a new feature called DARK MODE.
The DARK MODE is one of the greatest feature introduced by Apple in WWDC 19 with iOS 13.
We need to keep few points into our mind while integrating our app with Dark Mode feature.
All the above 4 colours to be used in same order with following 4 text respectively:
Usually we require to create the vibrancy effects in our app like:
Visual effect view blurring the background image
1 2 |
let blurEffect = UIBlurEffect(style: .systemThinMaterial) let blurView = UIVisualEffectView(effect: blurEffect) |
UIBlurEffect Style can be : systemThinMaterial, systemMaterial, systemMaterialDark, etc
Visual effect view for vibrancy
1 |
let vibrancyEffect = UIVibrancyEffect(blurEffect: blurEffect, style: .fill) |
The colours are will be provided in two different ways : White for Light Mode and Black for Dark Mode.
1 2 3 |
let dynamicColor = UIColor.systemBackground let traitCollection = view.traitCollection let resolvedColor = dynamicColor.resolvedColor(with: traitCollection) |
1 2 3 4 5 6 7 |
let dynamicColor = UIColor { (traitCollection: UITraitCollection) -> UIColor in if traitCollection.userInterfaceStyle == .dark { return .black } else { return .white } } |
1 2 3 4 5 6 |
class BackgroundView: UIView { override func draw(_ rect: CGRect) { // UIKit sets UITraitCollection.current to self.traitCollection UIColor.systemBackground.setFill() UIRectFill(rect) } |
1. Option 1
1 2 3 4 5 6 7 |
let layer = CALayer() let traitCollection = view.traitCollection // option 1 let resolvedColor = UIColor.label.resolvedColor(with: traitCollection) layer.borderColor = resolvedColor.cgColor |
2. Option 2
1 2 3 4 5 6 7 8 |
let layer = CALayer() let traitCollection = view.traitCollection // option 2 traitCollection.performAsCurrent { layer.borderColor = UIColor.label.cgColor } |
3. Option 3
1 2 3 4 5 6 7 |
let layer = CALayer() let traitCollection = view.traitCollection // option 3 UITraitCollection.current = traitCollection layer.borderColor = UIColor.label.cgColor |
UIScreen – > UIWindowScene – > UIWindow – > UIPresentationController – > UIViewController – > UIView
Traits are predicted during initialization
traitCollectionDidChange(_ 🙂 called only for changes
Enable debug logging with launch argument
-UITraitCollectionChangeLoggingEnabled YES
Layout is the best time to use traits
1 2 3 |
UIViewController.viewWillLayoutSubviews() UIView.layoutSubviews() UIViewController.viewDidLayoutSubviews() |
1 2 3 4 5 6 7 |
class UIViewController { var overrideUserInterfaceStyle: UIUserInterfaceStyle } class UIView { var overrideUserInterfaceStyle: UIUserInterfaceStyle } |
For entire app, set Info.plist key UIUserInterfaceStyle to Light or Dark
Existing API to override any traits
1 2 3 4 5 6 7 |
class UIPresentationController { var overrideTraitCollection: UITraitCollection? } class UIViewController { func setOverrideTraitCollection(_ : UITraitCollection?, forChild: UIViewController) } |
Only specify values for trait you want to override
Previously we have option .default
Now it splits into two options :
a) Light Mode : .darkContent
a) Dark Mode : .lightContent
Apple introduces dynamic styles :
a) .medium
b) .large
Usage of color property to set your own color
.gray, .white, .whiteLarge
Apple introduced the new way to draw attributed text from iOS 13,
1 2 3 4 |
let attributes: [NSAttributedString.key: Any] = [ .font: UIFont.systemFont(ofSize: 36.0) .foregroundColor: UIColor.label ] |
Declare supported color schemes with color-scheme
Use prefers-color-scheme media query for custom colors and images
For iPad apps:
a) API are similar
b) System Settings will be followed
c) Matches AppKit colors and materials
Apps on iOS 13 are expected to support dark mode
Use system colors and materials
Create your own dynamic colors and images
Leverage flexible infrastructure
Hope this will help you in getting updated for Dark Mode in iOS 13 with new features that will come around and it will help you to create your apps in Dark Mode.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.