美文网首页
暗黑模式适配

暗黑模式适配

作者: 得_道 | 来源:发表于2020-10-20 11:15 被阅读0次

    适配点

    • 不同模式的适配主要涉及颜色和图片两个方面的适配
    • 其中颜色适配包括相关背景色和字体颜色

    相关API

    各种枚举值

    typedef NS_ENUM(NSInteger, UIUserInterfaceStyle) {
        UIUserInterfaceStyleUnspecified,
        UIUserInterfaceStyleLight,
        UIUserInterfaceStyleDark,
    } API_AVAILABLE(tvos(10.0)) API_AVAILABLE(ios(12.0)) API_UNAVAILABLE(watchos);
    
    if (@available(iOS 13.0, *)) {
        UIUserInterfaceStyle mode = UITraitCollection.currentTraitCollection.userInterfaceStyle;
        if (mode == UIUserInterfaceStyleDark) {
            NSLog(@"深色模式");
        } else if (mode == UIUserInterfaceStyleLight) {
            NSLog(@"浅色模式");
        } else {
            NSLog(@"未知模式");
        }
    }
    

    监听系统的模式变化
    在iOS13系统中,UIViewController遵循两个协议:UITraitEnvironmentUIContentContainer协议
    在UITraitEnvironment协议中, 为我们提供了一个监听当前模式变化的方法

    @protocol UITraitEnvironment <NSObject>
    // 当前模式
    @property (nonatomic, readonly) UITraitCollection *traitCollection API_AVAILABLE(ios(8.0));
    
    // 重写该方法监听模式的改变
    - (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection API_AVAILABLE(ios(8.0));
    @end
    

    适配两个方面

    1. 颜色适配

    1.1UIColor

    • iOS13之前UIColor只能表示一种颜色,从iOS13开始UIColor是一个动态的颜色,在不同模式下可以分别代表不同的颜色
    • 下面是iOS13系统提供的动态颜色种类, 使用以下颜色值, 在模式切换时, 则不需要做特殊处理
    @interface UIColor (UIColorSystemColors)
    #pragma mark System colors
    
    @property (class, nonatomic, readonly) UIColor *systemRedColor          API_AVAILABLE(ios(7.0), tvos(9.0)) API_UNAVAILABLE(watchos);
    @property (class, nonatomic, readonly) UIColor *systemGreenColor        API_AVAILABLE(ios(7.0), tvos(9.0)) API_UNAVAILABLE(watchos);
    @property (class, nonatomic, readonly) UIColor *systemBlueColor         API_AVAILABLE(ios(7.0), tvos(9.0)) API_UNAVAILABLE(watchos);
    @property (class, nonatomic, readonly) UIColor *systemOrangeColor       API_AVAILABLE(ios(7.0), tvos(9.0)) API_UNAVAILABLE(watchos);
    @property (class, nonatomic, readonly) UIColor *systemYellowColor       API_AVAILABLE(ios(7.0), tvos(9.0)) API_UNAVAILABLE(watchos);
    @property (class, nonatomic, readonly) UIColor *systemPinkColor         API_AVAILABLE(ios(7.0), tvos(9.0)) API_UNAVAILABLE(watchos);
    @property (class, nonatomic, readonly) UIColor *systemPurpleColor       API_AVAILABLE(ios(9.0), tvos(9.0)) API_UNAVAILABLE(watchos);
    @property (class, nonatomic, readonly) UIColor *systemTealColor         API_AVAILABLE(ios(7.0), tvos(9.0)) API_UNAVAILABLE(watchos);
    @property (class, nonatomic, readonly) UIColor *systemIndigoColor       API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
    // 灰色种类, 在Light模式下, systemGray6Color更趋向于白色
    @property (class, nonatomic, readonly) UIColor *systemGrayColor         API_AVAILABLE(ios(7.0), tvos(9.0)) API_UNAVAILABLE(watchos);
    @property (class, nonatomic, readonly) UIColor *systemGray2Color        API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
    @property (class, nonatomic, readonly) UIColor *systemGray3Color        API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
    @property (class, nonatomic, readonly) UIColor *systemGray4Color        API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
    @property (class, nonatomic, readonly) UIColor *systemGray5Color        API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
    @property (class, nonatomic, readonly) UIColor *systemGray6Color        API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
    
    #pragma mark Foreground colors
    @property (class, nonatomic, readonly) UIColor *labelColor              API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
    @property (class, nonatomic, readonly) UIColor *secondaryLabelColor     API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
    @property (class, nonatomic, readonly) UIColor *tertiaryLabelColor      API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
    @property (class, nonatomic, readonly) UIColor *quaternaryLabelColor    API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
    // 系统链接的前景色
    @property (class, nonatomic, readonly) UIColor *linkColor               API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
    // 占位文字的颜色
    @property (class, nonatomic, readonly) UIColor *placeholderTextColor    API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
    // 边框或者分割线的颜色
    @property (class, nonatomic, readonly) UIColor *separatorColor          API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
    @property (class, nonatomic, readonly) UIColor *opaqueSeparatorColor    API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
    
    #pragma mark Background colors
    @property (class, nonatomic, readonly) UIColor *systemBackgroundColor                   API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
    @property (class, nonatomic, readonly) UIColor *secondarySystemBackgroundColor          API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
    @property (class, nonatomic, readonly) UIColor *tertiarySystemBackgroundColor           API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
    @property (class, nonatomic, readonly) UIColor *systemGroupedBackgroundColor            API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
    @property (class, nonatomic, readonly) UIColor *secondarySystemGroupedBackgroundColor   API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
    @property (class, nonatomic, readonly) UIColor *tertiarySystemGroupedBackgroundColor    API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
    
    #pragma mark Fill colors
    @property (class, nonatomic, readonly) UIColor *systemFillColor                         API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
    @property (class, nonatomic, readonly) UIColor *secondarySystemFillColor                API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
    @property (class, nonatomic, readonly) UIColor *tertiarySystemFillColor                 API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
    @property (class, nonatomic, readonly) UIColor *quaternarySystemFillColor               API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
    
    #pragma mark Other colors
    // 这两个是非动态颜色值
    @property(class, nonatomic, readonly) UIColor *lightTextColor API_UNAVAILABLE(tvos);    // for a dark background
    @property(class, nonatomic, readonly) UIColor *darkTextColor API_UNAVAILABLE(tvos);     // for a light background
    
    @property(class, nonatomic, readonly) UIColor *groupTableViewBackgroundColor API_DEPRECATED_WITH_REPLACEMENT("systemGroupedBackgroundColor", ios(2.0, 13.0), tvos(13.0, 13.0));
    @property(class, nonatomic, readonly) UIColor *viewFlipsideBackgroundColor API_DEPRECATED("", ios(2.0, 7.0)) API_UNAVAILABLE(tvos);
    @property(class, nonatomic, readonly) UIColor *scrollViewTexturedBackgroundColor API_DEPRECATED("", ios(3.2, 7.0)) API_UNAVAILABLE(tvos);
    @property(class, nonatomic, readonly) UIColor *underPageBackgroundColor API_DEPRECATED("", ios(5.0, 7.0)) API_UNAVAILABLE(tvos);
    
    @end
    
    • 上面系统提供的这些颜色种类, 根本不能满足我们正常开发的需要, 大部分的颜色值也都是自定义
    • 系统也为我们提供了创建自定义颜色的方法
    + (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);
    
    • 上面的方法接受一个闭包(block)
    • 当系统在LightMode和DarkMode之间相互切换时就会自动触发此回调
    • 回调返回一个UITraitCollection, 可根据改对象判断是那种模式

    1.2 CGColor

    • UIColor只是设置背景色和文字颜色的类, 可以动态的设置
    • 可是如果是需要设置类似边框颜色等属性时, 又该如何处理呢
    • 设置上述边框属性, 需要用到CGColor类, 但是在iOS13中CGColor并不是动态颜色值, 只能表示一种颜色
    • 在监听模式改变的方法中traitCollectionDidChange, 根据不同的模式进行处理
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        
        // 每次模式改变的时候, 这里都会执行
        if (previousTraitCollection?.userInterfaceStyle == .dark) {
            redView.layer.borderColor = UIColor.red.cgColor
        } else {
            redView.layer.borderColor = UIColor.green.cgColor
        }
    }
    

    2. 图片适配

    • 在iOS中, 图片基本都是放在Assets.xcassets里面, 所以图片的适配, 我们就相对麻烦一些了
    • 正常情况下都是下面这中处理方式


      image.png

    需要适配不同模式的情况下, 需要两套不同的图片, 并做如下设置
    在设置Appearances时, 我们选择Any, Dark就可以了(只需要适配深色模式和非深色模式)


    image.png

    特殊情况处理

    1. 当前页面

    • 原项目中如果没有适配Dark Mode, 当你切换到Dark Mode后, 你可能会发现, 有些部分页面的颜色自动适配了
    • 未设置过背景颜色或者文字颜色的组件, 在Dark Mode模式下, 就是黑色的
    • 这里我们就需要真对该单独App强制设置成Light Mode模式
    // 设置改属性, 只会影响当前的视图, 不会影响前面的controller和后续present的controller
    @available(iOS 13.0, *)
    open var overrideUserInterfaceStyle: UIUserInterfaceStyle
    
    // 使用示例
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        
        // 每次模式改变的时候, 这里都会执行
        if (previousTraitCollection?.userInterfaceStyle == .dark) {
            // 在Dark模式下, 强制改成Light模式
            overrideUserInterfaceStyle = .light
        }
    }
    

    2.强制项目的显示模式

    • 上面这种方式只能针对某一个页面修改, 如果需要对整个项目禁用Dark模式
    • 可以通过修改windowoverrideUserInterfaceStyle属性
    • 在Xcode11创建的项目中,windowAppDelegate移到SceneDelegate中, 添加下面这段代码, 就会做到全局修改显示模式
    let scene = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate
    scene?.window?.overrideUserInterfaceStyle = .light
    

    终极方案

    • 需要在info.plist文件中添加User Interface Style配置, 并设置为Light
    <key>UIUserInterfaceStyle</key>
    <string>Light</string>
    

    Status Bar更新

    iOS13中苹果对于Status Bar也做了部分修改, 在iOS13之前

    public enum UIStatusBarStyle : Int {
        case `default` // 默认文字黑色
    
        @available(iOS 7.0, *)
        case lightContent // 文字白色
    }
    

    iOS13开始UIStatusBarStyle一共有三种状态

    public enum UIStatusBarStyle : Int {
        case `default` // 自动选择黑色或白色
    
        @available(iOS 7.0, *)
        case lightContent // 文字白色
        
        @available(iOS 13.0, *)
        case darkContent // 文字黑色
    }
    

    相关文章

      网友评论

          本文标题:暗黑模式适配

          本文链接:https://www.haomeiwen.com/subject/fknrmktx.html