美文网首页iOS小技巧
iOS 适配DarkMode

iOS 适配DarkMode

作者: MonKey_Money | 来源:发表于2020-08-19 16:30 被阅读0次

    图片资源适配

    apple给我提供了图片资源的适配


    image.png

    我可以在不通的模式下,系统自动选择不同的图片资源

    颜色适配

    1.系统提供的颜色方法

    @interface UIColor (UIColorSystemColors)
    
    #pragma mark System colors
    
    /* Some colors that are used by system elements and applications.
     * These return named colors whose values may vary between different contexts and releases.
     * Do not make assumptions about the color spaces or actual colors used.
     */
    @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);
    
    /* Shades of gray. systemGray is the base gray color.
     */
    @property (class, nonatomic, readonly) UIColor *systemGrayColor         API_AVAILABLE(ios(7.0), tvos(9.0))
    

    2.自定义颜色

        UIColor *backgroundColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
                return [UIColor redColor];;
            } else {
                return [UIColor whiteColor];
    
            }
        }];
    

    模式配置

    关闭黑暗模式

    1.在Info.plist 文件中,添加 key 为 User Interface Style 类型String。
    2.将UIUserInterfaceStyle key 的值设置为 Light

    单页面关闭黑暗模式

    在ios13系统下UIViewcontroller与UIView有一个新的属性 overrideUserInterfaceStyle 可以强制设置关闭暗黑模式的属性

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

    调试切换黑暗模式

    storyboard中设置

    image.png

    菜单栏设置

    image.png

    相关文章

      网友评论

        本文标题:iOS 适配DarkMode

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