美文网首页OC
iOS 适配黑暗模式

iOS 适配黑暗模式

作者: 燃烧的大叔 | 来源:发表于2019-12-27 16:55 被阅读0次

    适配黑暗模式

    一直在UIViewController或者UIView中做实验traitCollectionDidChange,来来回回的搞了大半天,想找个不改变很多也不添加很多代码的方案。

    - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
    {
        NSLog(@"切换模式了");
    }
    

    下面的方法在不需要考虑你的APP有其他主题的时候。

    1. 创建一个Color管理类,比如YGColorManager
    2. 创建两个.plst文件,比如DayColor.plistNightColor.plist

    YGColorManager中创建两个获取plist文件的NSDictionary

    @property (nonatomic , strong) NSDictionary * dayColorDic;   /// 读取到的dayColor
    @property (nonatomic , strong) NSDictionary * nightColorDic;  /// 读取到的nightColor
    

    在两个.plist里面创建相同的color

    NightColor.plist

    截屏2019-12-27下午3.55.54.png

    DayColor.plist

    截屏2019-12-27下午3.55.35.png

    YGColorManager读取color

    - (UIColor *)YGWhite {
        return [self colorWithKey:@"YGWhite"];
    }
    
    - (UIColor *)colorWithKey:(NSString *)key{
        if (key == nil || key.length <= 0) {
            return [UIColor whiteColor];
        }
        if (@available(iOS 13.0, *)) {
            UIColor * color = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection)
            {
                /// 判断当前是深色还是浅色
                if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight)
                {
                    return [UIColor colorWithHex:[self.colorDic valueForKey:key]] ;
                }
                else if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark)
                {
                    return [UIColor colorWithHex:[self.nightColorDic valueForKey:key]];
                }
                return [UIColor whiteColor];
            }];
            return color;
        } 
        return [UIColor colorWithHex:[self.colorDic valueForKey:key]] ;
    }
    

    在外面使用直接使用

    xxx.backgroundColor = [YGColorManager sharedInstance].YGWhite;
    

    当你在手机设置中心设置选择浅色深色,都会改变颜色。只需用写一遍,不用在所有的color获取中中写下面重复的代码。
    并且你可以很轻松的管理你所使用到color

    if (@available(iOS 13.0, *)) {
            UIColor * color = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection)
            {
                ...
                return A_Color;
            }];
            return color;
        } 
    

    比如再添加颜色YGDark,在外面是直接使用就可以。

    - (UIColor *)YGDarkPink {
        return [self colorWithKey:@"YGDarkPink"];
    }
    

    不用plist

    我给朋友发过去说plist岂不是要维护两个plist,或许多了或者大项目有些不方便。不用plist又简介的,直接上代码。直接用个数组,或者- (UIColor *)colorWithArray:(NSArray *)colorArray改成- (UIColor *)colorWithDayColor:(UIColor *)dayColor nightColor:(UIColor *) nightColor相对应改变就可以了,这样你也可以再加个key运用这样方式改变主题。
    两个方法都可以。喜欢那个就用那个呗。

    - (UIColor *)YGTextWhite
    {
        return [self colorWithArray:@[@"#FFFFFF",@"#000000"]];
    }
    
    
    - (UIColor *)colorWithArray:(NSArray *)colorArray{
        if (colorArray == nil || colorArray.count == 0) {
            return [UIColor whiteColor];
        }
        if (@available(iOS 13.0, *)) {
            UIColor * color = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection)
            {
                if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight)
                {
                    return [UIColor colorWithHex:colorArray.firstObject] ;
                }
                else if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark)
                {
                    return  [UIColor colorWithHex:colorArray.lastObject];
                }
                return [UIColor whiteColor];
            }];
            return color;
        }
        return [UIColor whiteColor];
    }
    

    相关文章

      网友评论

        本文标题:iOS 适配黑暗模式

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