OC-适配暗黑模式

2021-09-09  本文已影响0人  紫云夕月
#import "PublicFunction.h"

@implementation PublicFunction
#pragma mark -- 适配暗黑模式颜色
+(UIColor *)generateDynamicColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor {
    if (@available(iOS 13.0, *)) {
        UIColor * dyColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
                return lightColor;
            }else {
                return darkColor;
            }
        }];
        return dyColor;
    }else{
        return lightColor;
    }
}

@end

#pragma mark -- 判断深色模式
+(BOOL)getUserInterfaceStyle {
    if (@available(iOS 13.0, *)) {
        UIUserInterfaceStyle mode = UITraitCollection.currentTraitCollection.userInterfaceStyle;
        if (mode == UIUserInterfaceStyleDark) {//深色模式
            return YES;
        } else if (mode == UIUserInterfaceStyleLight) {//浅色模式
            return NO;
        } else {
            return NO;
        }
    } else {//浅色模式
        return NO;
    }
}
//适配暗黑模式颜色
[PublicFunction generateDynamicColor:[UIColor blackColor] darkColor:[UIColor whiteColor]]

//判断深色模式
if ([PublicFunction getUserInterfaceStyle]) {//深色
       
} else {//浅色

}
上一篇 下一篇

猜你喜欢

热点阅读