暗黑模式(黑暗模式,深色模式)适配
打开Info.plist 项目配置文件,添加UIUserInterfaceStyle字段,key类型为String, 其value值设置为 Light模式。
iOS13之后UIViewController与UIView 都新增一个属性 overrideUserInterfaceStyle。该属性设置后,会影响视图控制器和子视图控制器以及父视图和子视图,但不会影响application和present出的视图控制器。
/* Set `overrideUserInterfaceStyle` to cause this view controller and its children to have a* specific `UIUserInterfaceStyle`. This does not affect the application or any parent view controllers.*/
@property (nonatomic) UIUserInterfaceStyle overrideUserInterfaceStyle API_AVAILABLE(tvos(13.0), ios(13.0)) API_UNAVAILABLE(watchos); // Defaults to UIUserInterfaceStyleUnspecified
颜色适配
- UIColor 动态颜色
systemBackgroundColor
、labelColor
、systemGray2Color
等,自己可以试试切换模式后会有什么效果。
if (@available(iOS 13.0, *)) {self.view.backgroundColor = [UIColor systemBackgroundColor];} else {// Fallback on earlier versionsself.view.backgroundColor = [UIColor whiteColor];}
- UIColor的
colorWithDynamicProvider
和initWithDynamicProvider
和resolvedColorWithTraitCollection
三个方法。
if (@available(iOS 13.0, *)) {col_lineView.backgroundColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {return [UIColor whiteColor];}else {return [UIColor blackColor];}}];} else {// Fallback on earlier versionscol_lineView.backgroundColor = [UIColor blackColor];}
图片适配
(1)在Assets.xcassets图片资源库里新建一个Image Set
(2)打开侧边栏,找到最后一项,设置Image Set属性Appearance为Any,Dark。
(3)修改图片名称(所有图片名都一样),拖动图片到不同模式下的对应图片资源里,1倍图可以不用
至此大功告成,可以运行看看效果。
监听模式切换
苹果提供了2个方法,一个是监听模式切换的方法,一个是判断当前的TraitCollection和不同的TraitCollection比较,查看是否能显示不同模式
/*! To be overridden as needed to provide custom behavior when the environment's traits change. */
- (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection API_AVAILABLE(ios(8.0));
/* Return whether this trait collection, compared to a different trait collection, could show a different appearance* for dynamic colors that are provided by UIKit or are in an asset catalog.* If you need to be aware of when dynamic colors might change, override `traitCollectionDidChange` in your view or view controller,* and use this method to compare `self.traitCollection` with `previousTraitCollection`.** Currently, a change in any of these traits could affect dynamic colors:* userInterfaceIdiom, userInterfaceStyle, displayGamut, accessibilityContrast, userInterfaceLevel* and more could be added in the future.*/
- (BOOL)hasDifferentColorAppearanceComparedToTraitCollection:(nullable UITraitCollection *)traitCollection API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
此方法进入后台时会调用2次,每次hasDifferentColorAppearanceComparedToTraitCollection
都返回YES。stackoverflow也有此问题
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {[super traitCollectionDidChange:previousTraitCollection];if (@available(iOS 13.0, *)) {BOOL isDiffernt = [self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection];if (isDiffernt) {NSLog(@"%ld----------%ld", (long)self.traitCollection.userInterfaceStyle, previousTraitCollection.userInterfaceStyle);}} else {// Fallback on earlier versions}
}