iOS-夜間模式(換膚設(shè)置)

iOS 開發(fā)中有時候會有夜間模式(換膚設(shè)置)的需求, 其實(shí)主要是更改相關(guān)顏色操作!

思路:每次切換夜間/白天模式時吮龄,都會發(fā)出通知給所有ViewController漓帚,讓它們切換到相應(yīng)的主題午磁。

  1. 創(chuàng)建一個管理模式主題的單例管理類ThemeManage
  1. 封裝好需要做夜間模式變色處理的控件擴(kuò)展:UIView (ThemeChange), UINavigationBar (ThemeChange), UITabBar (ThemeChange), UILabel (ThemeChange), UIButton (ThemeChange)
  2. 在 AppDelegate里先獲取夜間模式狀態(tài), 根控制器里先設(shè)置tabBar 及 子控制器里navigationBar的夜間模式狀態(tài)
  3. 添加控制白天/黑夜模式item,發(fā)通知切換相對應(yīng)i模式及image
  4. 添加相關(guān)控件是否黑夜模式下已更換字色和背景色

一. 創(chuàng)建一個管理模式主題的單例管理類ThemeManage

ThemeManage.h 文件:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface ThemeManage : NSObject

#pragma mark - 顏色屬性
@property(nonatomic, retain) UIColor *bgColor;
@property(nonatomic, retain) UIColor *color1;
@property(nonatomic, retain) UIColor *color2;
@property(nonatomic, retain) UIColor *textColor;
@property(nonatomic, retain) UIColor *textColorGray;
@property(nonatomic, retain) UIColor *navBarColor;
@property(nonatomic, retain) UIColor *colorClear;

#pragma mark -

// 是否是夜間 YES表示夜間, NO為正常
@property(nonatomic, assign) BOOL isNight;

/**
 * 模式管理單例
 */
+ (ThemeManage *)shareThemeManage;

@end

ThemeManage. m 文件

#import "ThemeManage.h"

static ThemeManage *themeManage; // 單例


@implementation ThemeManage

#pragma mark - 單例的初始化

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

#pragma mark 重寫isNight的set方法

- (void)setIsNight:(BOOL)isNight {
    
    _isNight = isNight;
    
    if (self.isNight) { // 夜間模式改變相關(guān)顏色
        
        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];
    });
}

@end

二. 封裝好需要做夜間模式變色處理的控件擴(kuò)展

一般需要UIView (ThemeChange), UINavigationBar (ThemeChange), UITabBar (ThemeChange), UILabel (ThemeChange), UIButton (ThemeChange);這里拿 UIView 做例子:

#import <UIKit/UIKit.h>

/**
 * 顏色狀態(tài)枚舉值 顏色的定義(一個代表一套)
 */
typedef NS_ENUM(NSInteger, UIViewColorType) {

    UIViewColorTypeNormal, // 白天白色, 夜間黑色
    UIViewColorType1, // 白天藍(lán)色, 夜間深灰
    UIViewColorType2, // 白天淺藍(lán), 夜間淺藍(lán)
    UIViewColorTypeClear // 透明狀態(tài)
};

@interface UIView (ThemeChange)

// 定義顏色類型的屬性, NSNumber類型
@property(nonatomic, assign) id type;

// 消息中心開始監(jiān)聽
- (void)startMonitor;
// 改變顏色的方法
- (void)changeColor;
// 設(shè)置顏色類型和對應(yīng)顏色
- (void)NightWithType:(UIViewColorType)type;

// 設(shè)置字體顏色的方法
- (void)initTextColor;

@end
#import "UIView+ThemeChange.h"
#import "ThemeManage.h"
// 添加runtime頭文件
#import <objc/runtime.h>

@implementation UIView (ThemeChange)

#pragma mark - 添加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));
}

#pragma mark - 開始監(jiān)聽

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

#pragma mark - 改變顏色

- (void)changeColor {
    // type為NSNumber型, 變?yōu)镹SInteger
    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;
    }
    
}

#pragma mark - 設(shè)置顏色類型和對應(yīng)顏色

- (void)NightWithType:(UIViewColorType)type {
    
    self.type = [NSNumber numberWithInteger:type];
    [self changeColor];
    [self startMonitor];
    
    // 調(diào)用設(shè)置字體顏色的方法
    [self initTextColor];
}

#pragma mark - 改變字體顏色的方法, 空方法, 可以在子類中重寫這個方法來改變顏色(例如:Label)

- (void)initTextColor {
    
}

@end

三. 在 AppDelegate里先獲取夜間模式狀態(tài), 根控制器里先設(shè)置tabBar 及 子控制器里navigationBar的夜間模式狀態(tài)

#import "ThemeManage.h"
#import "UIView+ThemeChange.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 獲取夜間模式狀態(tài)
    [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 文件:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view NightWithType:UIViewColorTypeNormal];
    
    HomeViewController *vc = [[HomeViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    // 設(shè)置navigationBar的夜間模式狀態(tài)
    [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];
    // 設(shè)置navigationBar的夜間模式狀態(tài)
    [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];
}

四. 添加控制白天/黑夜模式item,發(fā)通知切換相對應(yīng)i模式及image.

#import "ThemeManage.h"
#import "UIView+ThemeChange.h"
    [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:)];
#pragma mark  - Action點(diǎn)擊動作事件

// 切換夜間模式
- (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];
}

發(fā)了通知不要忘記移除監(jiān)聽

- (void)dealloc {
    // 移除監(jiān)聽
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

五. 添加相關(guān)控件是否黑夜模式下已更換字色和背景色

#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];

這時候測試下, 看下效果:

夜間模式.gif

Demo 下載請移步: 夜間模式(換膚設(shè)置)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末咕痛,一起剝皮案震驚了整個濱河市喇嘱,隨后出現(xiàn)的幾起案子婉称,更是在濱河造成了極大的恐慌构蹬,老刑警劉巖悔据,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件科汗,死亡現(xiàn)場離奇詭異,居然都是意外死亡怖亭,警方通過查閱死者的電腦和手機(jī)坤检,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進(jìn)店門早歇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人晨另,你說我怎么就攤上這事谱姓。” “怎么了垛玻?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵帚桩,是天一觀的道長嘹黔。 經(jīng)常有香客問我,道長郭蕉,這世上最難降的妖魔是什么喂江? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任获询,我火速辦了婚禮拐袜,結(jié)果婚禮上梢薪,老公的妹妹穿的比我還像新娘。我一直安慰自己秉撇,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布规阀。 她就那樣靜靜地躺著谁撼,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上与帆,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天玄糟,我揣著相機(jī)與錄音,去河邊找鬼阵翎。 笑死,一個胖子當(dāng)著我的面吹牛郭卫,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播贰军,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼词疼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了贰盗?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤陋率,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后赊窥,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體狸页,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年址遇,在試婚紗的時候發(fā)現(xiàn)自己被綠了倔约。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片坝初。...
    茶點(diǎn)故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖绢要,靈堂內(nèi)的尸體忽然破棺而出拗小,到底是詐尸還是另有隱情,我是刑警寧澤剿配,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布阅束,位于F島的核電站,受9級特大地震影響息裸,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜簿寂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一常遂、第九天 我趴在偏房一處隱蔽的房頂上張望挽荠。 院中可真熱鬧平绩,春花似錦漠另、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至算墨,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間报咳,已是汗流浹背挖藏。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留熬苍,地道東北人袁翁。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓粱胜,卻偏偏與公主長得像,于是被迫代替她去往敵國和親焙压。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內(nèi)容