IOS- night mode (skin set)

In the development of iOS sometimes night mode (skin settings) needs, it is mainly related to the color change operation!

IOS- night mode (skin set)

Idea: every time you switch the night / day mode, you will be notified to all ViewController to switch to the appropriate theme.

Single case management ThemeManage package to create a management mode need to do the theme of good night mode color processing control extension: UIView (ThemeChange), UINavigationBar (ThemeChange), UITabBar (ThemeChange), UILabel (ThemeChange), UIButton (ThemeChange) to obtain the night mode in AppDelegate, the root in the night mode first state controller set the tabBar and navigationBar sub controller to add the control day / night mode item, issued a notice to switch corresponding to I mode and image mode control is added under the night have replaced the word color and background color

Create a management model theme of a single case management class ThemeManage

ThemeManage.h file:

#import < Foundation/Foundation.h> #import < UIKit/UIKit.h> @interface; ThemeManage: NSObject #pragma mark - @property (nonatomic, retain color attributes) 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 - is YES / / night night, NO to @property (nonatomic, assign) BOOL isNight; / * * * * + management mode of single cases (ThemeManage * shareThemeManage); @end

ThemeManage. m file

#import "ThemeManage.h" static ThemeManage *themeManage @implementation ThemeManage #pragma; / / singleton mark - single cases initialization + (ThemeManage *) shareThemeManage dispatch_once_t onceToken {static; dispatch_once (& onceToken, themeManage [[ThemeManage alloc] ^{= init];}}); return themeManage; set #pragma mark isNight - rewriting method (void) (setIsNight: BOOL isNight) {_isNight = isNight; if (self.isNight) {/ / self.bgColor = night mode is related to the change of color [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]; self.bgColor = [UIColor whiteColor] else{}; self.textColor [UIColor = blackColor]; self.color1 = [UIColor colorWithRed:0.06 green: 0.25 blue:0.48 alpha:1] [UIColor colorWithRed:0.31 green:0.73 blue:0.58; self.navBarColor = alpha:1]; self.color2 = [UIColor colorWithRed:0.57 green:0.66 blue:0.77 alpha:1]; self.textColorGray [UIColor = grayColor];} static dispatch_once_t dispatch_once (& onceToken; onceToken [UIColor, ^{self.colorClear = clearColor];}}); @end

Two. Need to do a good night color pattern processing control expansion

Generally need UIView (ThemeChange), UINavigationBar (ThemeChange), UITabBar (ThemeChange), UILabel (ThemeChange), UIButton (ThemeChange); take UIView as an example:

#import < UIKit/UIKit.h> / * * * color state enumeration value color definition (a representative set) / typedef NS_ENUM (NSInteger, UIViewColorType) {UIViewColorTypeNormal / / day, night white, black UIViewColorType1, / / day night deep blue, gray UIViewColorType2, / / day blue night light blue transparent UIViewColorTypeClear / / state @interface (UIView}; ThemeChange) / / attribute definition of the color type, NSNumber type @property (nonatomic, assign) ID type; / / message center began to monitor (void) startMonitor; / / method to change the color of the - changeColor (void); / / set the color types and corresponding color - (void) NightWithType: (UIViewColorType) type; / / set the font color - (void) initTextColor; @end
#import "UIView+ThemeChange.h" #import "ThemeManage.h" / / add runtime header file #import < objc/runtime.h> @implementation; UIView (ThemeChange) #pragma mark - type set get added, - (void) setType: (ID type) {objc_setAssociatedObject (self, @selector (type), type, OBJC_ASSOCIATION_RETAIN_NONATOMIC);} - {type (ID) return objc_getAssociatedObject (self, @selector (type));} - #pragma mark - (void) startMonitor starts listening to "NSNotificationCenter defaultCenter] addObserver:self {selector:@selector (changeColor) name:@" changeColor "object:nil] #pragma mark;} - change color - (void) changeColor {/ / type NSNumber to 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 #pragma mark;}} - set the color types and corresponding color - (void) NightWithType: (UIViewColorType) type {self.type [NSNumber = numberWithInteger:type]; [self changeColor]; [self startMonitor]; / / call to set the font color Method [self initTextColor];} #pragma mark - change the font color method, empty method, can be overridden in subclasses to change the color of this method (for example: Label) - (void) initTextColor {} @end

Three. In the AppDelegate to get the night mode state, the root controller in the first set tabBar and the navigationBar controller in the night mode state

#import "ThemeManage.h" #import "UIView+ThemeChange.h"
- (BOOL) application: (UIApplication * application) didFinishLaunchingWithOptions: (NSDictionary * launchOptions) {/ / shareThemeManage].isNight = [ThemeManage for night mode [[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 file:

- (void) viewDidLoad viewDidLoad] [self.view {[super; NightWithType:UIViewColorTypeNormal]; HomeViewController *vc = [[HomeViewController alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc]; / / navigationBar [nav.navigationBar NightWithType:UIViewColorTypeNormal] vc.tabBarItem night mode; [[UITabBarItem alloc] = initWithTitle:@ "home page" image:[UIImage imageNamed:@ "home" tag:10]; SchemaViewController *secondVC = [[SchemaViewController alloc] init]; UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:secondVC]; / / set navigationBar mode [nav1.navigationBar NightWithType: night UIViewColorTypeNormal]; secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@ "menu" image:[UIImage imageNamed:@ "schema" tag:11] [self.tabBar; NightWithType:UIViewColorTypeNormal]; self.viewControllers = @[nav, nav1]; self.tabBar.translucent = NO; [[UINavigationBar appearance] setTranslucent:NO];}

Four. Add controls during the day / night mode item, send notifications to correspond to I mode and 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 - click event / night mode switching (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];}

Don’t forget to remove the listener

- (void) dealloc [[NSNotificationCenter defaultCenter] removeObserver:self] {/ / remove monitor;}

Five. Add the relevant controls if the color and background color has been changed in the night mode

#import "UILabel+ThemeChange.h"
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake (100, 100, 200, 40)]; label.text = @ "test to see word color and background color"; [label NightWithType:UIViewColorTypeNormal]; [label NightTextType:LabelColorGray]; [self.view addSubview: label];

This time under the test, see the effect:

IOS- night mode (skin set)
night mode.Gif