Step by step to achieve IOS theme skin switching effect _ios

Source: Internet
Author: User
Tags uikit

This example for you to share the iOS theme skin Switch code for your reference, the specific contents are as follows

1. Introduction to Theme Skin function switching
The theme switch is to dynamically change the user's interface according to the user's different theme, which usually changes the navigationbar background picture, tabbar background picture, Tabbar button picture and selected background picture, Navigationitem.title Font color for titles, other element controls in the UI

Download source code address: Http://xiazai.jb51.net/201609/yuanma/ThemeSkinSetup (jb51.net). rar

2. The project directory structure and the realization effect screenshot




3. Specific steps to achieve

1. Drag the Image folders (group) and skins into the resource folders in the Project project
2. Create Baseviewcontroller
3. Configure Theme.plist
4. The basic framework required for the project is available, and the TableView function of the subject is realized.
5. Create Theme Manager: Thememanager
6. Customizing the Themetabbaritem Control
7. Create a UI factory: Uifactory
8. Implement the Didselected event in TableView to complete the topic switch
9. Record the subject selected by the user so that when the user starts the next time it is the last set of topics

1. Create Baseviewcontroller

#import <UIKit/UIKit.h> 
@interface baseviewcontroller:uiviewcontroller 
 
-(void) reloadthemeimage; 
@end 

#import "BaseViewController.h" #import "ThemeManager.h" #import "NotificationMacro.h" @interface Baseviewcontrolle R () @end @implementation Baseviewcontroller-(ID) init {if (self = = [Super init]) {[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (themechangednotfication:) Name:kthemechangednotification Object 
 : nil]; 
 } [self reloadthemeimage]; 
return self; 
 }-(void) viewdidload {[Super viewdidload]; 
[Self reloadthemeimage]; 
 }-(void) didreceivememorywarning {[Super didreceivememorywarning]; 
Dispose of any of the can is recreated. 
}-(void) Themechangednotfication: (nsnotification *) notification {[Self reloadthemeimage]; 
  
 }-(void) reloadthemeimage {Thememanager * Thememanager = [Thememanager Sharedthememanager]; 
 UIImage * Navigationbackgroundimage = [Thememanager themeimagewithname:@ "Navigationbar_background.png"]; [Self.navigationController.navigationBar Setbackgroundimage:navigatiOnbackgroundimage Forbarmetrics:uibarmetricsdefault]; 
 UIImage * Tabbarbackgroundimage = [Thememanager themeimagewithname:@ "Tabbar_background.png"]; 
[Self.tabBarController.tabBar Setbackgroundimage:tabbarbackgroundimage]; 

 } @end

2. Realize Appdelegate

#import "AppDelegate.h" 
 
#import "MainViewController.h" 
#import "ThemeManager.h" 
#import " NotificationMacro.h " 
 
@interface appdelegate () 
 
@end 
 
@implementation appdelegate 
 
 
-(BOOL) Application: (uiapplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions { 
 [self Inituserdefaultconfig]; 
  
 Mainviewcontroller * Rootviewcontroller = [[Mainviewcontroller alloc] init]; 
 Self.window.rootViewController = Rootviewcontroller; 
  
 Return YES 
} 
 
 
-(void) Inituserdefaultconfig { 
 NSString * themename = [[Nsuserdefaults standarduserdefaults] Objectforkey: Kthemenamekey]; 
 Thememanager * Thememanager = [Thememanager Sharedthememanager]; 
 Thememanager.themename = ThemeName; 

<span style= "Font-weight:normal;" ><span style= "Font-weight:normal;" > #import "MainViewController.h" #import "HomeViewController.h" #import "MessageViewController.h" #import "Mineview  
 
Controller.h "#import" UIFactory.h "@interface Mainviewcontroller () @end @implementation Mainviewcontroller 
 -(ID) init {if (self = [super init]) {[Self Inittabbarui]; 
return self; 
  
}-(void) viewdidload {[Super viewdidload]; 
}-(void) didreceivememorywarning {[Super didreceivememorywarning]; 
 }-(void) Inittabbarui {//home Homeviewcontroller * Homeviewcontroller = [[Homeviewcontroller alloc] init]; Uinavigationcontroller * Homenavigationcontroller = [[Uinavigationcontroller alloc] Initwithrootviewcontroller: 
Homeviewcontroller]; Uitabbaritem * Hometabbaritem = [[Uitabbaritem alloc] initwithtitle:@ "Home" Image:[uiimage imagenamed:@ "Tabbar_home"] 
 Selectedimage:[uiimage imagenamed:@ "tabbar_home_selected"]; Uitabbaritem * HomeTabbaritem = [uifactory createtabbaritemwithtitle:@ Home "imagename:@" Tabbar_home "selectedimage:@" tabbar_home_ 
 Selected "]; 
  
 Homenavigationcontroller.tabbaritem = Hometabbaritem; 
 Message (center) messageviewcontroller * Messageviewcontroller = [[Messageviewcontroller alloc] init]; Uinavigationcontroller * Messagenavigationcontroller = [[Uinavigationcontroller alloc] Initwithrootviewcontroller: 
Messageviewcontroller]; Uitabbaritem * Messagetabbaritem = [[Uitabbaritem alloc] initwithtitle:@ ' message ' image:[uiimage imagenamed:@ ' tabbar_ 
 Message_center "] selectedimage:[uiimage imagenamed:@" tabbar_message_center_selected "]; Uitabbaritem * Messagetabbaritem = [uifactory createtabbaritemwithtitle:@ "message" imagename:@ "Tabbar_message_center" 
 selectedimage:@ "tabbar_message_center_selected"]; 
  
 Messagenavigationcontroller.tabbaritem = Messagetabbaritem; 
 I mineviewcontroller * Mineviewcontroller = [[Mineviewcontroller alloc] init]; Uinavigationcontroller * Minenavigationcontroller = [[UinaVigationcontroller alloc] Initwithrootviewcontroller:mineviewcontroller]; Uitabbaritem * Minetabbaritem = [[Uitabbaritem alloc] initwithtitle:@ "i" Image:[uiimage imagenamed:@ "Tabbar_profile" 
 ] Selectedimage:[uiimage imagenamed:@ "tabbar_profile_selected"]; Uitabbaritem * Minetabbaritem = [uifactory createtabbaritemwithtitle:@ "i" imagename:@ "Tabbar_profile" selectedImage:@ 
  
  
 "Tabbar_profile_selected"]; 
 Minenavigationcontroller.tabbaritem = Minetabbaritem; 
 Nsarray * viewcontrollers = @[homenavigationcontroller, Messagenavigationcontroller, MineNavigationController]; 
Self.viewcontrollers = viewcontrollers; 

 } @end

3. Create a Theme Manager

#import <Foundation/Foundation.h> 
#import <UIKit/UIKit.h> 
 
@interface thememanager:nsobject 
 
@property (nonatomic, copy) NSString * THEMENAME;   Subject name 
@property (nonatomic, retain) nsdictionary * THEMEPLISTDICT;//topic attribute list Dictionary 
 
+ (Thememanager *) Sharedthemema Nager; 
 
-(UIImage *) Themeimagewithname: (NSString *) imagename; 
@end </span></span> 

<span style= "Font-weight:normal;" ><span style= "Font-weight:normal;" > #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface thememanager:nsobject @prope   Rty (nonatomic, copy) NSString * THEMENAME; Subject name @property (nonatomic, retain) nsdictionary * THEMEPLISTDICT; 
 
Topic attribute List Dictionary + (Thememanager *) Sharedthememanager; 
-(UIImage *) Themeimagewithname: (NSString *) imagename; 
 
@end #import "ThemeManager.h" #import "NotificationMacro.h" Static Thememanager * Sharedthememanager;  @implementation Thememanager-(ID) init {if (self = [super init]) {NSString * Themepath = [[NSBundle Mainbundle] 
  pathforresource:@ "Theme" oftype:@ "Plist"]; 
  Self.themeplistdict = [Nsdictionary Dictionarywithcontentsoffile:themepath]; 
 Self.themename = nil; 
return self; } + (Thememanager *) Sharedthememanager {@synchronized (self) {if (nil = = Sharedthememanager) {Sharedthemema 
  Nager = [[Thememanager alloc] init]; } 
 } 
  
 return sharedthememanager; 
}//Override overrides the ThemeName set method-(void) Setthemename: (NSString *) themename {_themename = ThemeName; 
 }-(UIImage *) Themeimagewithname: (NSString *) imagename {if (imagename = = nil) {return nil; 
 } NSString * Themepath = [self themepath]; 
 NSString * Themeimagepath = [Themepath stringbyappendingpathcomponent:imagename]; 
  
 UIImage * Themeimage = [UIImage Imagewithcontentsoffile:themeimagepath]; 
return themeimage; 
 //Return Subject path-(NSString *) Themepath {NSString * resourcepath = [[NSBundle mainbundle] resourcepath]; if (Self.themename = = Nil | | [Self.themename isequaltostring:@ "]) 
 {return resourcepath; } NSString * Themesubpath = [self.themeplistdict objectForKey:self.themeName]; Skins/blue NSString * Themefilepath = [ResourcePath Stringbyappendingpathcomponent:themesubpath]; 
.../skins/blue return themefilepath; 

 } @end

4. Create Theme Buttons Themetabbaritem

#import <UIKit/UIKit.h> 
@interface themetabbaritem:uitabbaritem 
 
@property (nonatomic, copy) NSString * imagename; 
@property (nonatomic, copy) NSString * SELECTEDIMAGENAME; 
 
 
-(ID) Initwithtitle: (NSString *) title imagename: (NSString *) imagename selectedimage: (NSString *) selectedimagename; 
 
@end </span></span> 

<span style= "Font-weight:normal;" ><span style= "Font-weight:normal;" > #import "ThemeTabBarItem.h" #import "ThemeManager.h" #import "NotificationMacro.h" @implementation themetabbarite m///Registration Viewer-(ID) init {if (self = [super init]) {[[Nsnotificationcenter defaultcenter] addobserver:self SE 
 Lector: @selector (themechangednotification:) name:kthemechangednotification Object:nil]; 
return self; 
 }-(ID) Initwithtitle: (NSString *) title imagename: (NSString *) imagename selectedimage: (NSString *) Selectedimagename { 
  if (self = [self init]) {self.title = title;   Self.imagename = imagename; The [self setimagename:imagename]---> [self reloadthemeimage]--->[self setimage:image] are invoked at this time Self.selectedimagen 
 AME = selectedimagename;//call [self setselectedimagename:selectedimagename] at this time; 
return self; #pragma mark-#pragma mark-override Setter-(void) Setimagename: (NSString *) imagename {if (_imagename!= i 
  Magename) {_imagename = imagename; 
} [self reloadthemeimage]; }-(void) Setselectedimagename: (NSString *) Selectedimagename {if (_selectedimagename!= selectedimagename) {_se 
 Lectedimagename = Selectedimagename; 
} [self reloadthemeimage]; 
Reload the picture after the theme changes-(void) Themechangednotification: (nsnotification *) notification {[Self reloadthemeimage]; 
  
 }-(void) reloadthemeimage {Thememanager * Thememanager = [Thememanager Sharedthememanager]; 
  if (self.imagename!= nil) {UIImage * image = [Thememanager themeImageWithName:self.imageName]; 
 [Self setimage:image]; } if (Self.selectedimagename!= nil) {uiimage * selectedimage = [Thememanager themeImageWithName:self.selectedIma 
  Gename]; 
 [Self setselectedimage:selectedimage]; 
}-(void) Dealloc {[[Nsnotificationcenter defaultcenter] removeobserver:self]; 

 }

5. Create a UI Factory

#import <Foundation/Foundation.h> 
#import <UIKit/UIKit.h> 
 
@interface uifactory:nsobject 
 
+ (Uitabbaritem *) Createtabbaritemwithtitle: (NSString *) title imagename: (NSString *) imagename selectedimage: (NSString *) Selectedimagename; 
 
 
@end </span></span> 
<span style= "Font-weight:normal;" ><span style= "Font-weight:normal;" > #import <Foundation/Foundation.h> 
#import <UIKit/UIKit.h> 
 
@interface uifactory:nsobject 
 
+ (Uitabbaritem *) Createtabbaritemwithtitle: (NSString *) title imagename: (NSString *) imagename selectedimage: ( NSString *) Selectedimagename; 
 
 
@end 
#import "UIFactory.h" 
 
#import "ThemeTabBarItem.h" 
@implementation uifactory 
 
+ (Uitabbaritem *) Createtabbaritemwithtitle: (NSString *) title imagename: (NSString *) imagename selectedimage: (NSString *) selectedimagename { 
 Themetabbaritem * themetabbaritem = [[Themetabbaritem alloc] Initwithtitle:title imageName: ImageName Selectedimage:selectedimagename]; 
  
 return themetabbaritem; 
} 
@end 

6. Implementing events for selected cells

#import "BaseViewController.h" 
 
@interface mineviewcontroller:baseviewcontroller <uitableviewdelegate, Uitableviewdatasource> 
 
 
@property (weak, nonatomic) Iboutlet UITableView *tableview; 
 
@property (nonatomic, retain) nsmutablearray * THEMEDATASOURCE; 
@end 

#import "BaseViewController.h" @interface Mineviewcontroller:baseviewcontroller <uitableviewdelegate, Uitablevie 
 
Wdatasource> @property (Weak, nonatomic) Iboutlet UITableView *tableview; 
@property (nonatomic, retain) Nsmutablearray * THEMEDATASOURCE; @end #import "MineViewController.h" #import "ThemeManager.h" #import "NotificationMacro.h" @interface Mineviewcont 
 Roller () @end @implementation Mineviewcontroller-(void) viewdidload {[Super viewdidload]; 
  
 Self.title = @ "I"; 
 Thememanager * Thememanager = [Thememanager Sharedthememanager]; 
_themedatasource = [Nsmutablearray ArrayWithArray:themeManager.themePlistDict.allKeys]; 
 }-(void) didreceivememorywarning {[Super didreceivememorywarning]; 
Dispose of any of the can is recreated. #pragma mark-#pragma mark-uitableviewdelegate-(Nsinteger) TableView: (UITableView *) TableView numberofrowsi 
Nsection: (Nsinteger) section {return self.themeDataSource.count; } 
 
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath {static NSS 
 Tring * Identifier = @ "Cell"; 
 UITableViewCell * cell = [TableView dequeuereusablecellwithidentifier:identifier]; if (cell = = nil) {cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:identifier 
 ]; 
 } NSString * text = Self.themedatasource[indexpath.row]; 
  
 Cell.textLabel.text = text; 
 Thememanager * Thememanager = [Thememanager Sharedthememanager]; 
 NSString * Currenttheme = thememanager.themename; 
 if (Currenttheme = = nil) {Currenttheme = @ "Default"; 
 } if ([Currenttheme Isequaltostring:text]) {cell.accessorytype = Uitableviewcellaccessorycheckmark; 
 else {cell.accessorytype = Uitableviewcellaccessorynone; 
} return cell; }-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {Thememanager * 
Memanager = [Thememanager Sharedthememanager]; NSString * themename = Self.themedatasource[indexpath.row]; 
 if ([ThemeName isequaltostring:@ "Default"]) {themename = nil; 
 //Record the current subject name thememanager.themename = ThemeName; 
  
  
 [[Nsnotificationcenter Defaultcenter] postnotificationname:kthemechangednotification Object:nil]; 
 Theme Persistence Nsuserdefaults * userdefaults = [Nsuserdefaults standarduserdefaults]; 
 [Userdefaults Setobject:themename Forkey:kthemenamekey]; 
  
 [Userdefaults Synchronize]; 
Reload the data display Uitableviewcellaccessorycheckmark displays the checked checkmark v [Self.tableview Reloaddata];

 }

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.