概述

iOS 开发中有时候会有夜间模式(换肤设置)的需求, 主要是更改相关颜色操作每次切换夜间/白天模式时,都会发出通知给所有ViewController,让它们切换到相应的主题.

详细

代码下载:http://www.demodashi.com/demo/10668.html

一、实现功能及主要思路

实现功能:

iOS 开发中有时候会有夜间模式(换肤设置)的需求, 其实主要是更改相关颜色操作.每次切换夜间/白天模式时,都会发出通知给所有ViewController,让它们切换到相应的主题.

主要思路:

1. 创建一个管理模式主题的单例管理类ThemeManage

2. 封装好需要做夜间模式变色处理的控件扩展:UIView (ThemeChange), UINavigationBar (ThemeChange), UITabBar (ThemeChange), UILabel (ThemeChange), UIButton (ThemeChange)

3. 在 AppDelegate里先获取夜间模式状态, 根控制器里先设置tabBar 及 子控制器里navigationBar的夜间模式状态

4. 添加控制白天/黑夜模式item,发通知切换相对应i模式及image

5. 添加相关控件是否黑夜模式下已更换字色和背景色

二、程序实现

Step1. 创建一个管理模式主题的单例管理类

ThemeManage.h 文件里添加模式管理单例:

// 是否是夜间 YES表示夜间, NO为正常
@property(nonatomic, assign) BOOL isNight;
/*** 模式管理单例*/
+ (ThemeManage *)shareThemeManage;

ThemeManage. m 文件:

单例的初始化:

+ (ThemeManage *)shareThemeManage {static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{themeManage = [[ThemeManage alloc] init];});return themeManage;
}

重写isNight的set方法 (是否是夜间 YES表示夜间, NO为正常)

- (void)setIsNight:(BOOL)isNight {_isNight = isNight;if (self.isNight) { // 夜间模式改变相关颜色self.bgColor = [UIColor colorWithRed:0.06 green:0.08 blue:0.1 alpha:1];self.textColor = [UIColor whiteColor];self.color1 = [UIColor colorWithRed:0.08 green:0.11 blue:0.13 alpha:1];self.navBarColor = [UIColor whiteColor];self.color2 = [UIColor colorWithRed:0.2 green:0.31 blue:0.43 alpha:1];self.textColorGray = [UIColor whiteColor];} else{self.bgColor = [UIColor whiteColor];self.textColor = [UIColor blackColor];self.color1 = [UIColor colorWithRed:0.06 green:0.25 blue:0.48 alpha:1];self.navBarColor = [UIColor colorWithRed:0.31 green:0.73 blue:0.58 alpha:1];self.color2 = [UIColor colorWithRed:0.57 green:0.66 blue:0.77 alpha:1];self.textColorGray = [UIColor grayColor];}static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{self.colorClear = [UIColor clearColor];});
}

Step2. 封装好需要做夜间模式变色处理的控件扩展

一般需要UIView (ThemeChange), UINavigationBar (ThemeChange), UITabBar (ThemeChange), UILabel (ThemeChange), UIButton (ThemeChange);

详情见 Demo, 这里拿 UIView 做例子:

添加颜色状态枚举值 颜色的定义(一个代表一套):

typedef NS_ENUM(NSInteger, UIViewColorType) {UIViewColorTypeNormal, // 白天白色, 夜间黑色UIViewColorType1, // 白天蓝色, 夜间深灰UIViewColorType2, // 白天浅蓝, 夜间浅蓝UIViewColorTypeClear // 透明状态
};

添加type的set,get方法:

- (void)setType:(id)type {objc_setAssociatedObject(self, @selector(type), type, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (id)type {return objc_getAssociatedObject(self, @selector(type));
}

开始监听:

- (void)startMonitor {[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor) name:@"changeColor" object:nil];
}

改变颜色:

- (void)changeColor {// type为NSNumber型, 变为NSIntegerswitch ([self.type integerValue]) {case UIViewColorTypeNormal:self.backgroundColor = [ThemeManage shareThemeManage].bgColor;break;case UIViewColorType1:self.backgroundColor = [ThemeManage shareThemeManage].color1;break;case UIViewColorType2:self.backgroundColor = [ThemeManage shareThemeManage].color2;break;case UIViewColorTypeClear:self.backgroundColor = [ThemeManage shareThemeManage].colorClear;break;default:break;}}

设置颜色类型和对应颜色:

- (void)NightWithType:(UIViewColorType)type {self.type = [NSNumber numberWithInteger:type];[self changeColor];[self startMonitor];// 调用设置字体颜色的方法[self initTextColor];
}

改变字体颜色的方法, 空方法, 可以在子类中重写这个方法来改变颜色(例如:Label):

- (void)initTextColor {}

Step3. 在 AppDelegate里先获取夜间模式状态, 根控制器里先设置tabBar 及 子控制器里navigationBar的夜间模式状态

#import "ThemeManage.h"
#import "UIView+ThemeChange.h"

获取夜间模式状态:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// 获取夜间模式状态[ThemeManage shareThemeManage].isNight = [[NSUserDefaults standardUserDefaults] boolForKey:@"night"];self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];self.window.backgroundColor = [UIColor whiteColor];[self.window makeKeyAndVisible];RootViewController *rootVc = [[RootViewController alloc] init];self.window.rootViewController = rootVc;return YES;
}

RootViewController.m 文件里设置navigationBar的夜间模式状态:

- (void)viewDidLoad {[super viewDidLoad];[self.view NightWithType:UIViewColorTypeNormal];HomeViewController *vc = [[HomeViewController alloc] init];UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];// 设置navigationBar的夜间模式状态[nav.navigationBar NightWithType:UIViewColorTypeNormal];vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"home"] tag:10];SchemaViewController *secondVC = [[SchemaViewController alloc] init];UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:secondVC];// 设置navigationBar的夜间模式状态[nav1.navigationBar NightWithType:UIViewColorTypeNormal];secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"菜单" image:[UIImage imageNamed:@"schema"] tag:11];[self.tabBar NightWithType:UIViewColorTypeNormal];self.viewControllers = @[nav, nav1];self.tabBar.translucent = NO;[[UINavigationBar appearance] setTranslucent:NO];
}

Step4. 添加控制白天/黑夜模式item,发通知切换相对应i模式及image

    [self.view NightWithType:UIViewColorTypeNormal];UIImage *barButtonImage = [ThemeManage shareThemeManage].isNight ? [UIImage imageNamed:@"night"] : [UIImage imageNamed:@"day"];self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:barButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(rightBarBtnAction:)];

Action点击动作事件(切换夜间模式):

- (void)rightBarBtnAction:(UIBarButtonItem *)barButton {[ThemeManage shareThemeManage].isNight = ![ThemeManage shareThemeManage].isNight;[[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:nil];[[NSUserDefaults standardUserDefaults] setBool:[ThemeManage shareThemeManage].isNight forKey:@"night"];UIImage *barBtnImage = [ThemeManage shareThemeManage].isNight ? [UIImage imageNamed:@"night"] : [UIImage imageNamed:@"day"];[barButton setImage:barBtnImage];
}

发了通知不要忘记移除监听:

- (void)dealloc {// 移除监听[[NSNotificationCenter defaultCenter] removeObserver:self];
}

Step5. 添加相关控件是否黑夜模式下已更换字色和背景色

#import "UILabel+ThemeChange.h"
  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];label.text = @"测试看看字色及背景色";[label NightWithType:UIViewColorTypeNormal];[label NightTextType:LabelColorGray];[self.view addSubview:label];

三、项目截图及运行效果

项目截图:

这时候测试下, 看下运行效果:

夜间模式对比截图:

四、其他补充

界面性问题可以根据自己项目需求调整即可, 具体可参考代码, 项目能够直接运行!

注:本文著作权归作者,由demo大师(http://www.demodashi.com)宣传,拒绝转载,转载需要作者授权

iOS-夜间模式(换肤设置)相关推荐

  1. android ios 夜间模式切换,iOS-夜间模式(换肤设置)

    一.实现功能及主要思路 实现功能: iOS 开发中有时候会有夜间模式(换肤设置)的需求,  其实主要是更改相关颜色操作.每次切换夜间/白天模式时,都会发出通知给所有ViewController,让它们 ...

  2. iOS拓展---【转载】iOS客户端节日换肤方案探究

    [转载]iOS客户端节日换肤方案探究 一.前言: Tip: 本来这篇文章在圣诞节就已经准备好了,但是由于种种原因一直没有写完,今天将它写出来,也算是2018年的第一篇文章了.你好,2018! 过去圣诞 ...

  3. 小米手机夜间模式在哪设置?仅需2个步骤

    小米手机夜间模式在哪设置?当你的手机亮度太亮的时候,调低亮度也改变不了刺眼感的时候,最好的办法是什么呢?就是开启手机的夜间模式,本次来说说小米手机的夜间模式怎么设置吧! 说到夜间模式,一般情况下我们很 ...

  4. iOS客户端节日换肤方案探究

    转自:https://www.ianisme.com的博客 一.前言: tip: 本来这篇文章在圣诞节就已经准备好了,但是由于种种原因一直没有写完,今天将它写出来,也算是2018年的第一篇文章了.你好 ...

  5. 苹果WWDC 2019最全剧透抢先看:iOS夜间模式要来了!

    苹果全球开发者大会(WWDC)将于6月3日开幕.届时,该公司将推出一系列新的应用程序.功能和开发工具,并将对iPhone.iPad.Mac.Apple Watch和Apple TV等设备所运行的操作系 ...

  6. oppo计算机的夜间模式,OPPO如何设置夜间护眼模式?OPPO手机护眼模式使用教程

    手机已经是生活中必不可少的了,特别是在晚上,可玩多了会影响视力.虽然大部分的应用软件都有夜间模式,但是手机党们在退出应用时,往往被强光刺激,这对眼睛的伤害是非常大的.没想到R11已经自带夜间护眼模式喽 ...

  7. iOS客户端节日换肤的思考与实现

    最近单位的APP来了新的需求,市场说他们要在圣诞节把APP里的图标都换了,还要换背景图片,还要给部分view添加一个遮盖.对,就是换肤.以前没有搞过换肤,所以考虑了一天,感觉大概就是这么个思路,感觉有 ...

  8. iOS 夜间模式的实现

    我的第一款带有夜间模式功能的app终于出炉啦!!!! 首先说自己走了很多坑啊!最后发现这个第三方库挺好用啊!---------DKNightVersion 我就是结合这个库去处理各种控件的颜色改变以及 ...

  9. 如何设置Chrome夜间模式、如何设置Chrome背景色

    如何设置Chrome夜间模式(dark主题) 对于长时间用电脑的人来说黑色背景对眼睛较为良好 1. 在Chrome的网址栏里面输入"chrome://flags/" 2.搜索&qu ...

最新文章

  1. html js文本框文字列出,js实现文本框中输入文字页面中div层同步获取文本框内容的方法...
  2. DL之Keras:keras保存网络结构、网络拓扑图、网络模型(json、yaml、h5等)注意事项及代码实现
  3. HttpClient的释放问题
  4. 数理统计-5.1 总体与样本
  5. 五个有用的过滤器 (转)
  6. 【ACM2020】少样本学习综述
  7. 四十三、配置防盗链、访问控制Directory、访问控制FilesMatch
  8. Halcon对文件的创建、读取、写入、删除等操作
  9. JAVA上传文件 DiskFileUpload组件
  10. XML中写注释的格式
  11. 看看!挺动人的故事!!!
  12. 给oracle数据库现有表添加字段
  13. linux 绑定腾讯域名解析,一步步配置腾讯云服务器ubuntu 通过域名访问自己的网页tomcat(详细基础)...
  14. 计算机小白如何真正入门计算机?
  15. 2023秋招大厂经典面试题及答案整理归纳(161-180)校招必看
  16. 利用 Echarts 简单制作省份或区域地图步骤
  17. 后台执行linux命令
  18. SQL视图、存储过程、触发器、游标及完整性
  19. ostu阈值分割python实现_Otsu阈值OpenCV Python
  20. z77主板支持服务器内存条吗,支持更高规格的内存_华擎 Z77 极限玩家 4_主板评测-中关村在线...

热门文章

  1. VS Code调试C代码
  2. hashmap扩容线程安全问题_HashMap线程不安全的体现
  3. mysql sql 时间比较_mysql中sql语句进行日期比较
  4. ip层和4层的接口实现分析
  5. vector与list的接口介绍与如何使用以及区别,附代码。
  6. java switch嵌套if_(新手)Java课程作业,请各位老哥指教:综合运用嵌套if选择结构、switch选择结构、多重if选择结构实现商品换购功能...
  7. fastnest怎么一键排版_什么公众号排版编辑器可以换字体?公众号字体在哪里选择修改?...
  8. 实验大作业c语言实现简单ftp,C 语言实现 FTP 服务器
  9. 表单在线生成 html代码,JSP实现用于自动生成表单标签html代码的自定义表单标签...
  10. 《深入理解 Spring Cloud 与微服务构建》第四章 Dubbo