iOS 13 暗黑模式颜色适配

.h文件

#import 

#import 

NS_ASSUME_NONNULL_BEGIN

@interface color : NSObject

+(UIColor *)generateDynamicColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor;

@end

NS_ASSUME_NONNULL_END

.m文件

#import "color.h"

@implementation color


+(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

使用方法

self.view.backgroundColor = [color generateDynamicColor:[UIColor whiteColor] darkColor:[UIColor blackColor]];

你可能感兴趣的:(iOS 13 暗黑模式颜色适配)