iOS适配深色模式

2022-06-28  本文已影响0人  小羊爱学习

1.如果你的项目之前禁用了深色模式,在plist中请删除这一项


image.png
  1. UIColor
    iOS13中系统提供了动态颜色的方法:
+ (UIColor *)colorWithDynamicProvider:(UIColor * (^)(UITraitCollection *traitCollection))dynamicProvider API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
- (UIColor *)initWithDynamicProvider:(UIColor * (^)(UITraitCollection *traitCollection))dynamicProvider API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);

给UIColor 增加一个分类

+(UIColor *)colorWithDarkModeColor:(UIColor *)darkColor normalColor:(UIColor *)color{
    if (@available(iOS 13.0,*)) {
        if (darkColor) {
            UIColor *dyColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull trainCollection) {
                if ([trainCollection userInterfaceStyle] == UIUserInterfaceStyleDark) {
                    return darkColor;
                }
                else {
                    return color;
                }
            }];
            return dyColor;
        }
        return color;
    }
    return color;
}
上一篇下一篇

猜你喜欢

热点阅读