iOS开发中适配iOS13新增的暗黑模式
iOS开发中适配iOS13新增的暗黑模式
iOS开发中适配iOS13新增的暗黑模式
禁用App的暗黑模式
由于我们没有对iOS13新增的暗黑模式进行适配,我们可以禁用App的暗黑模式。
在info.plist文件中增加User Interface Style并设置为Light。
禁用App某些页面的暗黑模式
如果我们对App某些页面适配了暗黑模式,有些页面还没来得及适配,我们可以对某些页面禁用暗黑模式。
self.view.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
适配颜色
iOS13设置颜色增加了方法
/* Create a dynamic color with a provider. * When methods are called on this color that need color component values, * the provider is called with UITraitCollection.currentTraitCollection. * The provider should use that trait collection to decide a more fundamental UIColor to return. * As much as possible, use the given trait collection to make that decision, not other state. */ + (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);
我们可以通过 拓展 @interface UIColor (TBColorExtend) 在拓展类里判断当前的模式,然后设置不同模式下的颜色。
如下,在#import "UIColor+TBColorExtend.h"
//适配iOS13,暗黑模式
+(UIColor *)generateDynamicColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor;
#import "UIColor+TBColorExtend.m"
@implementation UIColor (TBColorExtend)
//适配iOS13,暗黑模式
+(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;
}
}
如此调用即可全集变换颜色。简单快捷。。。。



















































































