热门标签 | HotTags
当前位置:  开发笔记 > IOS > 正文

一步一步实现iOS主题皮肤切换效果

这篇文章主要为大家详细介绍了一步一步实现iOS主题皮肤切换效果的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

下载源代码地址: http://xiazai.jb51.net/201609/yuanma/ThemeSkinSetup(jb51.net).rar

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  
@interface BaseViewController : UIViewController 
 
- (void) reloadThemeImage; 
@end 

#import "BaseViewController.h" 
 
#import "ThemeManager.h" 
#import "NotificationMacro.h" 
 
@interface BaseViewController () 
 
@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 resources that can be 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. 实现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; 
} 

#import "MainViewController.h" 
 
#import "HomeViewController.h" 
#import "MessageViewController.h" 
#import "MineViewController.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 { 
 // 主页 
 HomeViewController * homeViewCOntroller= [[HomeViewController alloc] init]; 
 UINavigationController * homeNavigatiOnController= [[UINavigationController alloc] initWithRootViewController:homeViewController]; 
// UITabBarItem * homeTabBarItem = [[UITabBarItem alloc] initWithTitle:@"主页" image:[UIImage imageNamed:@"tabbar_home"] selectedImage:[UIImage imageNamed:@"tabbar_home_selected"]]; 
 UITabBarItem * homeTabBarItem = [UIFactory createTabBarItemWithTitle:@"主页" imageName:@"tabbar_home" selectedImage:@"tabbar_home_selected"]; 
 homeNavigationController.tabBarItem = homeTabBarItem; 
  
 // 消息(中心) 
 MessageViewController * messageViewCOntroller= [[MessageViewController alloc] init]; 
 UINavigationController * messageNavigatiOnController= [[UINavigationController alloc] initWithRootViewController:messageViewController]; 
// UITabBarItem * messageTabBarItem = [[UITabBarItem alloc] initWithTitle:@"消息" image:[UIImage imageNamed:@"tabbar_message_center"] selectedImage:[UIImage imageNamed:@"tabbar_message_center_selected"]]; 
 UITabBarItem * messageTabBarItem = [UIFactory createTabBarItemWithTitle:@"消息" imageName:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"]; 
 messageNavigationController.tabBarItem = messageTabBarItem; 
  
 // 我 
 MineViewController * mineViewCOntroller= [[MineViewController alloc] init]; 
 UINavigationController * mineNavigatiOnController= [[UINavigationController alloc] initWithRootViewController:mineViewController]; 
// UITabBarItem * mineTabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:[UIImage imageNamed:@"tabbar_profile"] selectedImage:[UIImage imageNamed:@"tabbar_profile_selected"]]; 
 UITabBarItem * mineTabBarItem = [UIFactory createTabBarItemWithTitle:@"我" imageName:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"]; 
  
  
 mineNavigationController.tabBarItem = mineTabBarItem; 
 NSArray * viewCOntrollers= @[homeNavigationController, messageNavigationController, mineNavigationController]; 
 self.viewCOntrollers= viewControllers; 
} 
 
 
@end 

3. 创建主题管理器

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

#import  
#import  
 
@interface ThemeManager : NSObject 
 
@property (nonatomic, copy) NSString * themeName;   // 主题名字 
@property (nonatomic, retain) NSDictionary * themePlistDict; // 主题属性列表字典 
 
+ (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) { 
   sharedThemeManager = [[ThemeManager alloc] init]; 
  } 
 } 
  
 return sharedThemeManager; 
} 
 
// Override 重写themeName的set方法 
- (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; 
} 
 
// 返回主题路径 
- (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. 创建主题按钮 ThemeTabBarItem

#import  
@interface ThemeTabBarItem : UITabBarItem 
 
@property (nonatomic, copy) NSString * imageName; 
@property (nonatomic, copy) NSString * selectedImageName; 
 
 
- (id) initWithTitle:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImageName; 
 
@end  

#import "ThemeTabBarItem.h" 
#import "ThemeManager.h" 
#import "NotificationMacro.h" 
 
@implementation ThemeTabBarItem 
 
// 初始化时注册观察者 
- (id) init { 
 if (self = [super init]) { 
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@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;   // 此时会调用[self setImageName:imageName] ---> [self reloadThemeImage] --->[self setImage:image] 
  self.selectedImageName = selectedImageName;// 此时会调用[self setSelectedImageName:selectedImageName]; 
 } 
  
 return self; 
} 
 
 
#pragma mark - 
#pragma mark - Override Setter 
- (void) setImageName:(NSString *)imageName { 
 if (_imageName != imageName) { 
  _imageName = imageName; 
 } 
  
 [self reloadThemeImage]; 
} 
 
- (void) setSelectedImageName:(NSString *)selectedImageName { 
 if (_selectedImageName != selectedImageName) { 
  _selectedImageName = selectedImageName; 
 } 
  
 [self reloadThemeImage]; 
} 
 
 
 
// 主题改变之后重新加载图片 
- (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.selectedImageName]; 
  [self setSelectedImage:selectedImage]; 
 } 
} 
 
- (void) dealloc { 
 [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

5. 创建UI工厂

#import  
#import  
 
@interface UIFactory : NSObject 
 
+ (UITabBarItem *) createTabBarItemWithTitle:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImageName; 
 
 
@end 
#import  
#import  
 
@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. 实现选中单元格的事件

#import "BaseViewController.h" 
 
@interface MineViewController : BaseViewController  
 
 
@property (weak, nonatomic) IBOutlet UITableView *tableView; 
 
@property (nonatomic, retain) NSMutableArray * themeDataSource; 
@end 

#import "BaseViewController.h" 
 
@interface MineViewController : BaseViewController  
 
 
@property (weak, nonatomic) IBOutlet UITableView *tableView; 
 
@property (nonatomic, retain) NSMutableArray * themeDataSource; 
@end 
#import "MineViewController.h" 
 
#import "ThemeManager.h" 
#import "NotificationMacro.h" 
 
@interface MineViewController () 
 
@end 
 
@implementation MineViewController 
 
- (void)viewDidLoad { 
 [super viewDidLoad]; 
 self.title = @"我"; 
  
 ThemeManager * themeManager = [ThemeManager sharedThemeManager]; 
 _themeDataSource = [NSMutableArray arrayWithArray:themeManager.themePlistDict.allKeys]; 
} 
 
- (void)didReceiveMemoryWarning { 
 [super didReceiveMemoryWarning]; 
 // Dispose of any resources that can be recreated. 
} 
 
 
 
#pragma mark - 
#pragma mark - UITableViewDelegate 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
 
 return self.themeDataSource.count; 
} 
 
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
 static NSString * 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 = @"默认"; 
 } 
 if ([currentTheme isEqualToString:text]) { 
  cell.accessoryType = UITableViewCellAccessoryCheckmark; 
 } else { 
  cell.accessoryType = UITableViewCellAccessoryNone; 
 } 
  
 return cell; 
} 
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
  
 ThemeManager * themeManager = [ThemeManager sharedThemeManager]; 
 NSString * themeName = self.themeDataSource[indexPath.row]; 
  
 if ([themeName isEqualToString:@"默认"]) { 
  themeName = nil; 
 } 
  
 // 记录当前主题名字 
 themeManager.themeName = themeName; 
 [[NSNotificationCenter defaultCenter] postNotificationName:kThemeChangedNotification object:nil]; 
  
  
 // 主题持久化 
 NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]; 
 [userDefaults setObject:themeName forKey:kThemeNameKey]; 
 [userDefaults synchronize]; 
  
 // 重新加载数据显示UITableViewCellAccessoryCheckmark 显示选中的对号 v 
 [self.tableView reloadData]; 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
  • 当iOS设备越狱后,某些插件可能会导致系统崩溃(白苹果)。此时,可以通过进入安全模式来排查并删除有问题的插件。本文将详细介绍如何通过特定按键组合进入不加载MobileSubstrate的安全模式,并提供相关背景知识。 ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 本文将详细介绍在Windows 7环境下,检查U盘启动盘是否制作成功的多种方法,包括通过BIOS设置和使用模拟启动工具。 ... [详细]
  • 本文介绍如何使用 NSTimer 实现倒计时功能,详细讲解了初始化方法、参数配置以及具体实现步骤。通过示例代码展示如何创建和管理定时器,确保在指定时间间隔内执行特定任务。 ... [详细]
  • 本文介绍了在Windows环境下使用pydoc工具的方法,并详细解释了如何通过命令行和浏览器查看Python内置函数的文档。此外,还提供了关于raw_input和open函数的具体用法和功能说明。 ... [详细]
  • 题目Link题目学习link1题目学习link2题目学习link3%%%受益匪浅!-----&# ... [详细]
  • 本文探讨了 C++ 中普通数组和标准库类型 vector 的初始化方法。普通数组具有固定长度,而 vector 是一种可扩展的容器,允许动态调整大小。文章详细介绍了不同初始化方式及其应用场景,并提供了代码示例以加深理解。 ... [详细]
  • 网络运维工程师负责确保企业IT基础设施的稳定运行,保障业务连续性和数据安全。他们需要具备多种技能,包括搭建和维护网络环境、监控系统性能、处理突发事件等。本文将探讨网络运维工程师的职业前景及其平均薪酬水平。 ... [详细]
  • 高效解决应用崩溃问题!友盟新版错误分析工具全面升级
    友盟推出的最新版错误分析工具,专为移动开发者设计,提供强大的Crash收集与分析功能。该工具能够实时监控App运行状态,快速发现并修复错误,显著提升应用的稳定性和用户体验。 ... [详细]
  • 本题涉及一棵由N个节点组成的树(共有N-1条边),初始时所有节点均为白色。题目要求处理两种操作:一是改变某个节点的颜色(从白变黑或从黑变白);二是查询从根节点到指定节点路径上的第一个黑色节点,若无则输出-1。 ... [详细]
  • 并发编程:深入理解设计原理与优化
    本文探讨了并发编程中的关键设计原则,特别是Java内存模型(JMM)的happens-before规则及其对多线程编程的影响。文章详细介绍了DCL双重检查锁定模式的问题及解决方案,并总结了不同处理器和内存模型之间的关系,旨在为程序员提供更深入的理解和最佳实践。 ... [详细]
  • 技术人员转型项目管理:常见思维误区与挑战解析
    本文探讨了技术人员在向项目管理角色转变过程中常见的思维误区和困惑,分析了如何有效管理项目中的事务和人员,提供了实用的解决方案。 ... [详细]
  • 基于KVM的SRIOV直通配置及性能测试
    SRIOV介绍、VF直通配置,以及包转发率性能测试小慢哥的原创文章,欢迎转载目录?1.SRIOV介绍?2.环境说明?3.开启SRIOV?4.生成VF?5.VF ... [详细]
  • 探索1000以内的完美数:因数和等于自身
    本文探讨了如何在1000以内找到所有完美数,即一个数的因数(不包括自身)之和等于该数本身。例如,6是一个完美数,因为1 + 2 + 3 = 6。通过编程实现这一过程,可以更好地理解完美数的特性。 ... [详细]
author-avatar
caozhizhao
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有