配色: 字号:
一步一步实现iOS主题皮肤切换效果
2016-12-08 | 阅:  转:  |  分享 
  
一步一步实现iOS主题皮肤切换效果

本文实例为大家分享了iOS主题皮肤切换代码,供大家参考,具体内容如下

1.主题皮肤功能切换介绍主题切换就是根据用户设置不同的主题,来动态改变用户的界面,通常会改变navigationBar背景图片、tabBar背景图片、tabBar中的按钮的图片和选中的背景图片、navigationItem.title标题的字体颜色、UI中其他元素控件



2.项目目录结构及实现效果截图

















3.具体实现步骤



1.将image文件夹(group)和Skins拖入到项目工程中的资源文件夹中

2.创建BaseViewController

3.配置theme.plist

4.事项项目所需的基本框架供能,并实现主题的tableView功能

5.创建主题管理器:ThemeManager

6.自定义ThemeTabBarItem控件

7.创建UI工厂:UIFactory

8.实现tableView中的didSelected事件完成主题切换

9.记录用户选择的主题,以便用户下次启动时是上次设置的主题



1.创建BaseViewController



#import@interfaceBaseViewController:UIViewController-(void)reloadThemeImage;@end



#import"BaseViewController.h"#import"ThemeManager.h"#import"NotificationMacro.h"@interfaceBaseViewController()@end@implementationBaseViewController-(id)init{if(self==[superinit]){[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(themeChangedNotfication:)name:kThemeChangedNotificationobject:nil];}[selfreloadThemeImage];returnself;}-(void)viewDidLoad{[superviewDidLoad];[selfreloadThemeImage];}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];//Disposeofanyresourcesthatcanberecreated.}-(void)themeChangedNotfication:(NSNotification)notification{[selfreloadThemeImage];}-(void)reloadThemeImage{ThemeManagerthemeManager=[ThemeManagersharedThemeManager];UIImagenavigationBackgroundImage=[themeManagerthemeImageWithName:@"navigationbar_background.png"];[self.navigationController.navigationBarsetBackgroundImage:navigationBackgroundImageforBarMetrics:UIBarMetricsDefault];UIImagetabBarBackgroundImage=[themeManagerthemeImageWithName:@"tabbar_background.png"];[self.tabBarController.tabBarsetBackgroundImage:tabBarBackgroundImage];}@end



2.实现AppDelegate



#import"AppDelegate.h"#import"MainViewController.h"#import"ThemeManager.h"#import"NotificationMacro.h"@interfaceAppDelegate()@end@implementationAppDelegate-(BOOL)application:(UIApplication)applicationdidFinishLaunchingWithOptions:(NSDictionary)launchOptions{[selfinitUserDefaultConfig];MainViewControllerrootViewController=[[MainViewControlleralloc]init];self.window.rootViewController=rootViewController;returnYES;}-(void)initUserDefaultConfig{NSStringthemeName=[[NSUserDefaultsstandardUserDefaults]objectForKey:kThemeNameKey];ThemeManagerthemeManager=[ThemeManagersharedThemeManager];themeManager.themeName=themeName;}



#import"MainViewController.h"#import"HomeViewController.h"#import"MessageViewController.h"#import"MineViewController.h"#import"UIFactory.h"@interfaceMainViewController()@end@implementationMainViewController-(id)init{if(self=[superinit]){[selfinitTabBarUI];}returnself;}-(void)viewDidLoad{[superviewDidLoad];}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];}-(void)initTabBarUI{//主页HomeViewControllerhomeViewController=[[HomeViewControlleralloc]init];UINavigationControllerhomeNavigationController=[[UINavigationControlleralloc]initWithRootViewController:homeViewController];//UITabBarItemhomeTabBarItem=[[UITabBarItemalloc]initWithTitle:@"主页"image:[UIImageimageNamed:@"tabbar_home"]selectedImage:[UIImageimageNamed:@"tabbar_home_selected"]];UITabBarItemhomeTabBarItem=[UIFactorycreateTabBarItemWithTitle:@"主页"imageName:@"tabbar_home"selectedImage:@"tabbar_home_selected"];homeNavigationController.tabBarItem=homeTabBarItem;//消息(中心)MessageViewControllermessageViewController=[[MessageViewControlleralloc]init];UINavigationControllermessageNavigationController=[[UINavigationControlleralloc]initWithRootViewController:messageViewController];//UITabBarItemmessageTabBarItem=[[UITabBarItemalloc]initWithTitle:@"消息"image:[UIImageimageNamed:@"tabbar_message_center"]selectedImage:[UIImageimageNamed:@"tabbar_message_center_selected"]];UITabBarItemmessageTabBarItem=[UIFactorycreateTabBarItemWithTitle:@"消息"imageName:@"tabbar_message_center"selectedImage:@"tabbar_message_center_selected"];messageNavigationController.tabBarItem=messageTabBarItem;//我MineViewControllermineViewController=[[MineViewControlleralloc]init];UINavigationControllermineNavigationController=[[UINavigationControlleralloc]initWithRootViewController:mineViewController];//UITabBarItemmineTabBarItem=[[UITabBarItemalloc]initWithTitle:@"我"image:[UIImageimageNamed:@"tabbar_profile"]selectedImage:[UIImageimageNamed:@"tabbar_profile_selected"]];UITabBarItemmineTabBarItem=[UIFactorycreateTabBarItemWithTitle:@"我"imageName:@"tabbar_profile"selectedImage:@"tabbar_profile_selected"];mineNavigationController.tabBarItem=mineTabBarItem;NSArrayviewControllers=@[homeNavigationController,messageNavigationController,mineNavigationController];self.viewControllers=viewControllers;}@end



3.创建主题管理器



#import#import@interfaceThemeManager:NSObject@property(nonatomic,copy)NSStringthemeName;//主题名字@property(nonatomic,retain)NSDictionarythemePlistDict;//主题属性列表字典+(ThemeManager)sharedThemeManager;-(UIImage)themeImageWithName:(NSString)imageName;@end



#import#import@interfaceThemeManager:NSObject@property(nonatomic,copy)NSStringthemeName;//主题名字@property(nonatomic,retain)NSDictionarythemePlistDict;//主题属性列表字典+(ThemeManager)sharedThemeManager;-(UIImage)themeImageWithName:(NSString)imageName;@end#import"ThemeManager.h"#import"NotificationMacro.h"staticThemeManagersharedThemeManager;@implementationThemeManager-(id)init{if(self=[superinit]){NSStringthemePath=[[NSBundlemainBundle]pathForResource:@"theme"ofType:@"plist"];self.themePlistDict=[NSDictionarydictionaryWithContentsOfFile:themePath];self.themeName=nil;}returnself;}+(ThemeManager)sharedThemeManager{@synchronized(self){if(nil==sharedThemeManager){sharedThemeManager=[[ThemeManageralloc]init];}}returnsharedThemeManager;}//Override重写themeName的set方法-(void)setThemeName:(NSString)themeName{_themeName=themeName;}-(UIImage)themeImageWithName:(NSString)imageName{if(imageName==nil){returnnil;}NSStringthemePath=[selfthemePath];NSStringthemeImagePath=[themePathstringByAppendingPathComponent:imageName];UIImagethemeImage=[UIImageimageWithContentsOfFile:themeImagePath];returnthemeImage;}//返回主题路径-(NSString)themePath{NSStringresourcePath=[[NSBundlemainBundle]resourcePath];if(self.themeName==nil||[self.themeNameisEqualToString:@""]){returnresourcePath;}NSStringthemeSubPath=[self.themePlistDictobjectForKey:self.themeName];//Skins/blueNSStringthemeFilePath=[resourcePathstringByAppendingPathComponent:themeSubPath];//.../Skins/bluereturnthemeFilePath;}@end



4.创建主题按钮ThemeTabBarItem



#import@interfaceThemeTabBarItem:UITabBarItem@property(nonatomic,copy)NSStringimageName;@property(nonatomic,copy)NSStringselectedImageName;-(id)initwww.visa158.comWithTitle:(NSString)titleimageName:(NSString)imageNameselectedImage:(NSString)selectedImageName;@end



#import"ThemeTabBarItem.h"#import"ThemeManager.h"#import"NotificationMacro.h"@implementationThemeTabBarItem//初始化时注册观察者-(id)init{if(self=[superinit]){[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(themeChangedNotification:)name:kThemeChangedNotificationobject:nil];}returnself;}-(id)initWithTitle:(NSString)titleimageName:(NSString)imageNameselectedImage:(NSString)selectedImageName{if(self=[selfinit]){self.title=title;self.imageName=imageName;//此时会调用[selfsetImageName:imageName]--->[selfreloadThemeImage]--->[selfsetImage:image]self.selectedImageName=selectedImageName;//此时会调用[selfsetSelectedImageName:selectedImageName];}returnself;}#pragmamark-#pragmamark-OverrideSetter-(void)setImageName:(NSString)imageName{if(_imageName!=imageName){_imageName=imageName;}[selfreloadThemeImage];}-(void)setSelectedImageName:(NSString)selectedImageName{if(_selectedImageName!=selectedImageName){_selectedImageName=selectedImageName;}[selfreloadThemeImage];}//主题改变之后重新加载图片-(void)themeChangedNotification:(NSNotification)notification{[selfreloadThemeImage];}-(void)reloadThemeImage{ThemeManagerthemeManager=[ThemeManagersharedThemeManager];if(self.imageName!=nil){UIImageimage=[themeManagerthemeImageWithName:self.imageName];[selfsetImage:image];}if(self.selectedImageName!=nil){UIImageselectedImage=[themeManagerthemeImageWithName:self.www.hunanwang.netselectedImageName];[selfsetSelectedImage:selectedImage];}}-(void)dealloc{[[NSNotificationCenterdefaultCenter]removeObserver:self];}



5.创建UI工厂



#import#import@interfaceUIFactory:NSObject+(UITabBarItem)createTabBarItemWithTitle:(NSString)titleimageName:(NSString)imageNameselectedImage:(NSString)selectedImageName;@end



#import#import@interfaceUIFactory:NSObject+(UITabBarItem)createTabBarItemWithTitle:(NSString)titleimageName:(NSString)imageNameselectedImage:(NSString)selectedImageName;@end#import"UIFactory.h"#import"ThemeTabBarItem.h"@implementationUIFactory+(UITabBarItem)createTabBarItemWithTitle:(NSString)titleimageName:(NSString)imageNameselectedImage:(NSString)selectedImageName{ThemeTabBarItemthemeTabBarItem=[[ThemeTabBarItemalloc]initWithTitle:titleimageName:imageNameselectedImage:selectedImageName];returnthemeTabBarItem;}@end



6.实现选中单元格的事件



#import"BaseViewController.h"@interfaceMineViewController:BaseViewController@property(weak,nonatomic)IBOutletUITableViewtableView;@property(nonatomic,retain)NSMutableArraythemeDataSource;@end



#import"BaseViewController.h"@interfaceMineViewController:BaseViewController@property(weak,nonatomic)IBOutletUITableViewtableView;@property(nonatomic,retain)NSMutableArraythemeDataSource;@end#import"MineViewController.h"#import"ThemeManager.h"#import"NotificationMacro.h"@interfaceMineViewController()@end@implementationMineViewController-(void)viewDidLoad{[superviewDidLoad];self.title=@"我";ThemeManagerthemeManager=[ThemeManagersharedThemeManager];_themeDataSource=[NSMutableArrayarrayWithArray:themeManager.themePlistDict.allKeys];}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];//Disposeofanyresourcesthatcanberecreated.}#pragmamark-#pragmamark-UITableViewDelegate-(NSInteger)tableView:(UITableView)tableViewnumberOfRowsInSection:(NSInteger)section{returnself.themeDataSource.count;}-(UITableViewCell)tableView:(UITableView)tableViewcellForRowAtIndexPath:(NSIndexPath)indexPath{staticNSStringIdentifier=@"Cell";UITableViewCellcell=[tableViewdequeueReusableCellWithIdentifier:Identifier];if(cell==nil){cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:Identifier];}NSStringtext=self.themeDataSource[indexPath.row];cell.textLabel.text=text;ThemeManagerthemeManager=[ThemeManagersharedThemeManager];NSStringcurrentTheme=themeManager.themeName;if(currentTheme==nil){currentTheme=@"默认";}if([currentThemeisEqualToString:text]){cell.accessoryType=UITableViewCellAccessoryCheckmark;}else{cell.accessoryType=UITableViewCellAccessoryNone;}returncell;}-(void)tableView:(UITableView)tableViewdidSelectRowAtIndexPath:(NSIndexPath)indexPath{ThemeManagerthemeManager=[ThemeManagersharedThemeManager];NSStringthemeName=self.themeDataSource[indexPath.row];if([themeNameisEqualToString:@"默认"]){themeName=nil;}//记录当前主题名字themeManager.themeName=themeName;[[NSNotificationCenterdefaultCenter]postNotificationName:kThemeChangedNotificationobject:nil];//主题持久化NSUserDefaultsuserDefaults=[NSUserDefaultsstandardUserDefaults];[userDefaultssetObject:themeNameforKey:kThemeNameKey];[userDefaultssynchronize];//重新加载数据显示UITableViewCellAccessoryCheckmark显示选中的对号v[self.tableViewreloadData];}























献花(0)
+1
(本文系白狐一梦首藏)