暗黑模式(黑暗模式,深色模式)适配

暗黑模式(黑暗模式,深色模式)适配

  • 全局关闭

打开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 动态颜色systemBackgroundColorlabelColorsystemGray2Color等,自己可以试试切换模式后会有什么效果。
    if (@available(iOS 13.0, *)) {
        self.view.backgroundColor = [UIColor systemBackgroundColor];
    } else {
        // Fallback on earlier versions
        self.view.backgroundColor = [UIColor whiteColor];
    }

  • UIColor的colorWithDynamicProviderinitWithDynamicProviderresolvedColorWithTraitCollection三个方法。
    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 versions
        col_lineView.backgroundColor = [UIColor blackColor];
    }

图片适配

(1)在Assets.xcassets图片资源库里新建一个Image Set


新建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
    }
}

你可能感兴趣的:(暗黑模式(黑暗模式,深色模式)适配)