iOS 收藏篇iOS

iOS13-应用适配暗黑模式

2019-09-25  本文已影响0人  329fd8af610c

1.环境

Xcode11 正式版
iOS13 beta版本
Mac系统: 10.15 beta版本

2.原理

3.资源适配

4. UIColor 适配

iOS13之前的UIColor 只能返回一种特定的颜色,iOS13及其以后可以返回动态的颜色
UIColor增加了两个初始化方法,使用以下方法可以创建动态UIColor
注:一个是类方法,一个是实例方法,两个方法如下:

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

typedef NS_ENUM(NSInteger, UIUserInterfaceStyle) {
    UIUserInterfaceStyleUnspecified,
    UIUserInterfaceStyleLight,
    UIUserInterfaceStyleDark,
}

示例代码🌰

-(void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    NSLog(@"traitCollectionDidChange");
   //创建动态 color
    UIColor *color = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
        if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
            return [UIColor darkGrayColor];
        } else {
            return [UIColor redColor];
        }

    }];
    self.view.backgroundColor = color;

}

5.CGColor 适配

iOS13后,UIColor能够表示动态颜色,但是CGColor依然只能表示一种颜色,那么对于CALayer等对象如何适配暗黑模式呢?
有几下三种方式进行表示

方法如下:

-(void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    NSLog(@"traitCollectionDidChange");
    if (@available(iOS 13.0, *)) {
        //创建动态 bgColor
        UIColor *bgColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
                return [UIColor blackColor];
            } else {
                return [UIColor whiteColor];
            }
        }];
        self.view.backgroundColor = bgColor;
        //创建动态 layerColor
        UIColor *layerColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
                return [UIColor darkGrayColor];
            } else {
                return [UIColor redColor];
            }
        }];
        //改变layer 颜色
            //1.方法一
        //    UIColor *resolveColor = [layerColor resolvedColorWithTraitCollection:self.traitCollection];
        //    layer.backgroundColor = resolveColor.CGColor;
            //2.方法二
        //    [self.traitCollection performAsCurrentTraitCollection:^{
        //        UIColor *resolveColor = [layerColor resolvedColorWithTraitCollection:self.traitCollection];
        //        layer.backgroundColor = resolveColor.CGColor;
        //    }];
            //3.方法三
            layer.backgroundColor = layerColor.CGColor;
    } else {
        NSLog(@"不是iOS13 所以不需要暗黑模式");
    }
}

6.更新方法

当用户更改外观时,系统会通知所有window与View需要更新样式,在此过程中iOS会触发以下方法, 完整的触发方法文档

UIView:中可以调用如下方法进行更改

traitCollectionDidChange(_:)
layoutSubviews()
draw(_:)
updateConstraints()
tintColorDidChange()

UIViewController:中可以调用如下方法进行更改

traitCollectionDidChange(_:)
updateViewConstraints()
viewWillLayoutSubviews()
viewDidLayoutSubviews()

UIPresentationController:中可以调用如下方法进行更改

traitCollectionDidChange(_:)
containerViewWillLayoutSubviews()
containerViewDidLayoutSubviews()

7.暗黑适配几种情况

1> 默认情况下应用不做任何处理是需要跟随手机系统做暗黑适配
2> 如果整个应用中坚持是Light 或者Dark模式,需要在info.plist
中 添加 UIUserInterfaceStyleLight 或者Dark
3> 部分UIView / UIViewcontroller / UIWindow 不跟随手机暗黑适配

a.UIViewController与UIView 都新增一个属性 -overrideUserInterfaceStyle
b.将 -overrideUserInterfaceStyle 设置为对应的模式,则强制限制该元素与其子元素以设置的模式进行展示,不跟随系统模式改变进行改变
c.-overrideUserInterfaceStyle影响范围

设置 ViewController 的该属性, 将会影响视图控制器的视图和子视图控制器采用该样式,不影响弹出的vc
设置 View 的该属性, 将会影响视图及其所有子视图采用该样式
设置 Window 的该属性, 将会影响窗口中的所有内容都采用样式,包括根视图控制器和在该窗口中显示内容的所有演示控制器(UIPresentationController)

/*
* When set on an ordinary `UIView`:
 * - This property affects only the traits of this view and its subviews.
 * - It does not affect any view controllers, or any subviews that are owned by different view controllers.
 *
 * When set on a `UIWindow`:
 * - This property affects the `rootViewController` and thus the entire view controller and view hierarchy.
 * - It also affects presentations that happen inside the window.
 */
@property (nonatomic) UIUserInterfaceStyle overrideUserInterfaceStyle API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);

8.模式切换打印

模式切换时自动打印log,就不需要我们一次又一次的执行po命令了
在Xcode菜单栏Product->Scheme->Edit Scheme
选择Run->Arguments->Arguments Passed On Launch
添加以下命令即可
-UITraitCollectionChangeLoggingEnabled YES

图三

当切换模式就会在控制台打印出信息,信息如下:


图四

9.资料借鉴

demo: https://github.com/sisios/DarkModel.git
WWDC视频:https://developer.apple.com/videos/play/wwdc2019/214/
https://www.jianshu.com/p/7925bd51d2d6
https://www.jianshu.com/p/e6616e44cf60

10.项目实战:

我们项目用的DKNightVersion三方适配夜间模式,为了更好的兼容iOS13暗黑模式,我在第一个页签的viewcontroller的重写*-(void)traitCollectionDidChange:(UITraitCollection )previousTraitCollection方法

-(void)applicationDidFinishLaunching:(UIApplication *)application {
    //暗黑模式
    if (@available(iOS 13.0, *)) {
        UIUserInterfaceStyle style = UITraitCollection.currentTraitCollection.userInterfaceStyle;
        if (style == UIUserInterfaceStyleDark) {
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"showNightMode"];
           
            [self.dk_manager nightFalling];
        } else {
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"showNightMode"];
            [self.dk_manager dawnComing];
        }
    } else {
        NSLog(@"不是iOS13,不需要暗黑模式");
    }
}
//暗黑模式
-(void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    NSLog(@"traitCollectionDidChange");
   //创建动态 color
    if (@available(iOS 13.0, *)) {
        UIColor *color = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"showNightMode"];
                
                [self.dk_manager nightFalling];
                return [UIColor blackColor];
            } else {
                [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"showNightMode"];
                
                [self.dk_manager dawnComing];
                return [UIColor whiteColor];
            }
        }];
        self.view.backgroundColor = color;
    } else {
        NSLog(@"不是iOS13版本不需要暗黑模式");
    }

}

以上就是我总记得iOS13适配暗黑模式的总结,如有错误,不吝赐教!

上一篇下一篇

猜你喜欢

热点阅读