Dark Mode
principle
- Create two styles of the same resource. The system automatically obtains the resources of the currently selected style.
- Every time the system updates the style, the application will call all existing elements to call the corresponding re-methods to redraw the view. You can make corresponding changes to the corresponding method.
Resource file adaptation
- Create an Assets file (or in an existing Assets file)
- Create a new image resource file (or color resource file, or other resource file)
- Select the resource file, open the Xcode ->View ->Inspectors ->Show Attributes Inspectors (or Option+Command+4) view, change the Apperances option to Any, Dark
- After performing the third step, the resource file will have multiple container boxes, namely Any Apperance and Dark Apperance. Any Apperance is applied to the default situation (Unspecified) and highlight situation (Light), and Dark Apperance is applied to the dark mode (Dark)
- When the code is executed by default, it can be used normally by name. The system will automatically obtain the corresponding resource file according to the current mode.
Notice
After multiple Assets files in the same project are packaged, a file will be generated, so it is necessary to ensure that the names of resource files in Assets cannot be the same.
How to adapt colors in code (UIColor)
+ (UIColor *)colorWithDynamicProvider:(UIColor * (^)(UITraitCollection *))dynamicProvider API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos); - (UIColor *)initWithDynamicProvider:(UIColor * (^)(UITraitCollection *))dynamicProvider API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
.
[UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull trait) { if ( == UIUserInterfaceStyleDark) { return UIColorRGB(0x000000); } else { return UIColorRGB(0xFFFFFF); } }];
System call update method, custom redraw view
When the user changes the appearance, the system will notify all windows and Views that need to update the style. During this process, iOS will trigger the following method.Complete trigger method documentation
UIView
traitCollectionDidChange(_:) layoutSubviews() draw(_:) updateConstraints() tintColorDidChange()
UIViewController
traitCollectionDidChange(_:) updateViewConstraints() viewWillLayoutSubviews() viewDidLayoutSubviews()
UIPresentationController
traitCollectionDidChange(_:) containerViewWillLayoutSubviews() containerViewDidLayoutSubviews()
How to adapt without system switching styles
Notice
Apple strongly recommends adapting to Dark Mode. This function is also for developers to slowly adapt their applications to Dark Mode.
So if you want to use this function to not adapt to the dark mode, you are expected to be rejected
Turn off dark mode globally
- In the file, add the UIUserInterfaceStyle key name as User Interface Style value is String,
- Set the value of UIUserInterfaceStyle key to Light
Single interface does not follow dark mode
- Both UIViewController and UIView add a new attribute overrideUserInterfaceStyle
- Set overrideUserInterfaceStyle to the corresponding mode, and force the element and its child elements to be displayed in the set mode, and do not change according to the system mode change.
- Setting this property of ViewController will affect the view controller's view and subview controller adopt this style
- Setting this property of the View will affect the view and all its subviews to adopt this style.
- Setting this property of the Window will affect the style of everything in the window, including the root view controller and all demonstration controllers that display content in the window (UIPresentationController)
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.