一步一步实现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 <UIKit/UIKit.h>
@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;
}</span></span> 
<span style="font-weight: normal;"><span style="font-weight: normal;">#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 <Foundation/Foundation.h>
#import <UIKit/UIKit.h> 

@interface ThemeManager : NSObject 

@property (nonatomic, copy) NSString * themeName;   // 主题名字
@property (nonatomic, retain) NSDictionary * themePlistDict; // 主题属性列表字典 

+ (ThemeManager *) sharedThemeManager; 

- (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 

@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 <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 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 <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. 实现选中单元格的事件

#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, UITableViewDataSource> 

@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];
}

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

时间: 2016-09-28

iOS开发之App主题切换解决方案完整版(Swift版)

本篇博客就来介绍一下iOS App中主题切换的常规做法,当然本篇博客中只是提到了一种主题切换的方法,当然还有其他方法,在此就不做过多赘述了.本篇博客中所涉及的Demo完全使用Swift3.0编写完成,并使用iOS的NSNotification来触发主题切换的动作.本篇博客我们先对我们的主题系统进行设计,然后给出具体实现方式.当然在我们设计本篇博客所涉及的Demo时,我们要遵循"高内聚,低耦合","面向接口编程","便于维护与扩充"等特点. 本篇博

iOS开发之APP相关

AppDelegate对象方法 复制代码 代码如下: # 程序第一次启动后才会执行 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions; # 程序将要失去焦点时执行 - (void)applicationWillResignActive:(UIApplication *)application; # 程序得到焦点时调用 - (voi

IOS开发之UIScrollView实现图片轮播器的无限滚动

IOS开发之UIScrollView实现图片轮播器的无限滚动 简介 在现在的一些App中常常见到图片轮播器,一般用于展示广告.新闻等数据,在iOS内并没有现成的控件直接实现这种功能,但是通过UIScrollView的允许分页设置,可以实现滚动轮播的功能. 轮播原理 UIScrollView对象有pagingEnable成员,如果设置为YES,那么每一个scrollView尺寸这么大的区域就会被当作一页,在滚动时会根据滚动的比例自动计算应该切换到哪一页. 无限滚动原理 要实现无限滚动,需要额外的两

IOS 开发之xcode对比两个分支中同一个文件

IOS 开发之xcode对比两个分支中同一个文件 对于同一个项目的两个分支,因为两个分支可能各自都做了一些修改,所以通过Source Control中的History...功能是无法查看的,如下图: 这个时候,我们需要用到xcode的另外一个功能,如图: 操作步骤: 1.点击图中标注为1红框中的按钮,中间的界面会出现并排的两个窗口3和4(窗口3为当前分支文件内容). 2.从另外一个分支的xcode窗口中选中同名的文件,然后使用"command+`"切换到当前的xcode窗口. 3.拖动

IOS 开发之PickerView文字和随机数的使用

IOS 开发之PickerView文字和随机数的使用 PickerView用于展示供选择的内容(例如日期选取.点菜等). 有三种情况: 1.每一列都是独立的选取 2.右边的列受到左边列的影响 3.包含图片 PickerView和TableView类似,通过数据源来显示数据,与TableView同样地,让控制器称为其数据源. 但是PickerView的数据源仅仅提供行数和列数,在代理方法内才能设置内容. 通过两个数据源方法设置行和列数,通过一个代理方法来设定内容,注意component表示第几列:

iOS开发之UIKeyboardTypeNumberPad数字键盘自定义按键

最近做一个搜索用户的功能,这里使用了UISearchBar.由于搜索的方式只有手机号码,所以这里的键盘要限制为数字输入,可以这么做: self.searchBar.keyboardType = UIKeyboardTypeNumberPad;如果使用的不是搜索框而是textField输入框,可以设置textField的键盘属性来展示 self.textField.keyboardType = UIKeyboardTypeNumberPad;监听事件如下所示即可. 但是这里有个问题,就是数字键盘上

IOS开发之@property的详细介绍

IOS开发之@property的详细介绍 在类中定义属性时,总会使用到@property进行定义,下面就来说说@property的使用. 在使用过程中,如果需求公开且在其他类中使用时,通常会定义在.h头文件中:而如果只是该类自已需要使用,这时则会定义 在.m实现文件中. 使用格式 @property (参数1, 参数2, 参数3, ...) 参数类型 参数名称 参数包括三个种类,七个属性,如下图所示. 在使用过程中,有几点需要注意: (1)原子性定义中,如果没有涉及到多线程环境时,通常不会使用默

IOS 开发之swift中UIView的扩展使用的实例

IOS 开发之swift中UIView的扩展使用的实例 扩展类代码: import UIKit extension UIView { // MARK : 坐标尺寸 var origin:CGPoint { get { return self.frame.origin } set(newValue) { var rect = self.frame rect.origin = newValue self.frame = rect } } var size:CGSize { get { return

iOS开发之UITableView与UISearchController实现搜索及上拉加载,下拉刷新实例代码

废话不多说了,直接给大家贴代码了. 具体代码如下所示: #import "ViewController.h" #import "TuanGouModel.h" #import "TuanGouTableViewCell.h" #define kDeviceWidth [UIScreen mainScreen].bounds.size.width #define kDeviceHeight [UIScreen mainScreen].bounds.

IOS 开发之swift中手势的实例详解

IOS 开发之swift中手势的实例详解 手势操作主要包括如下几类 手势 属性 说明 点击 UITapGestureRecognizer numberOfTapsRequired:点击的次数:numberOfTouchesRequired:点击时有手指数量 设置属性 numberOfTapsRequired 可以实现单击,或双击的效果 滑动 UISwipeGestureRecognizer direction:滑动方向 direction 滑动方向分为上Up.下Down.左Left.右Right