概述
詳細
一、實現功能及主要思路
實現功能:
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型, 變為NSInteger switch ([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大師發表,拒絕轉載,轉載需要作者授權