iOS开发之主题皮肤
最近在开发一款【公交应用】,里面有个模块涉及到主题设置,这篇文章主要谈一下个人的做法。
大概的步骤如下:
(1):整个应用依赖于一个主题管理器,主题管理器根据当前的主题配置,加载不同主题文件夹下的主题
(2):在应用的各个Controller中,涉及到需要更换主题图片或颜色的地方,由原来的硬编码方式改为从主题管理器获取(此处可以看到,虽然.xib配置UI会比编码渲染UI效率来得高,但在灵活性以及协同开发方面还是有些弱了)
(3):在主题设置Controller中,一旦切换主题,则需要像系统的通知中心发送消息(这一步的目的,主要是为了让那些已经存在的并且UI已构建完成的对象修改他们的主题)
最终的效果图见:
https://github.com/yanghua/iBus#version-102preview
首先来看看主题文件夹的目录结构:
可以看到,通常情况下主题都是预先做好的,当然了,这里因为我没有后端server,如果你的应用是拥有后端server的,那么可以做得更加强大,比如不将主题文件存储在Bundle中,而是存储在应用sandBox的Document中。这样你在应用启动的时候就可以check server 是否有新的主题包,如果有就download下来,释放到Document,这样会方便许多,而不需要在升级之后才能够使用新主题。扯远了,这里可以看到这些文件夹是蓝颜色的,而非黄颜色,可见它不是xcode project的Group,它们都是真实存储的文件夹(它们也是app的bundle的一部分,只是获取的方式有些不同罢了,这样做的目的就是为了方便组织各个主题的文件、目录结构)。
看看获取主题文件夹路径的方式:
- #define Bundle_Of_ThemeResource @"ThemeResource"
- //the path in the bundle
- #define Bundle_Path_Of_ThemeResource \
- [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:Bundle_Of_ThemeResource]
ThemeManager:
- @interface ThemeManager : NSObject
- @property (nonatomic,copy) NSString *themeName;
- @property (nonatomic,copy) NSString *themePath;
- @property (nonatomic,retain) UIColor *themeColor;
- + (ThemeManager*)sharedInstance;
- - (NSString *)changeTheme:(NSString*)themeName;
- - (UIImage*)themedImageWithName:(NSString*)imgName;
- @end
可以看到,ThemeManager对外开放了三个属性以及两个实例方法,并且ThemeManager被构建为是单例的(Single)
- - (id)init{
- if (self=[super init]) {
- [self initCurrentTheme];
- }
- return self;
- }
initCurrentTheme定义:
- - (void)initCurrentTheme{
- self.themeName=[ConfigItemDao get:@"主题设置"];
- NSString *themeColorStr=[ConfigItemDao get:self.themeName];
- self.themeColor=[UIColor parseColorFromStr:themeColorStr];
- self.themePath=[Bundle_Path_Of_ThemeResource stringByAppendingPathComponent:self.themeName];
- //init UI
- UIImage *navBarBackgroundImg=[[self themedImageWithName:@"themeColor.png"]
- resizableImageWithCapInsets:UIEdgeInsetsMake(0.0f, 0.0f, 1.0f, 1.0f)
- resizingMode:UIImageResizingModeTile];
- [[UINavigationBar appearance] setBackgroundImage:navBarBackgroundImg
- forBarMetrics:UIBarMetricsDefault];
- }
这里从sqlite中获取了主题类型、主题颜色,然后配置了主题属性,同时初始化了一致的UINavigationBar。
- UIImage *img=[UIImage imageNamed:@"img.png"];
取而代之的是:
- UIImage *img=[[ThemeManager sharedInstance] themedImageWithName@"img.png"];
来确保img.png是来自当前主题文件夹下的img.png
- - (UIImage*)themedImageWithName:(NSString*)imgName{
- if (!imgName || [imgName isEqualToString:@""]) {
- return nil;
- }
- NSString *imgPath=[self.themePath stringByAppendingPathComponent:imgName];
- return [UIImage imageWithContentsOfFile:imgPath];
- }
到主题设置界面,选择一个新主题,会调用changeTheme方法:
- - (NSString *)changeTheme:(NSString*)themeName{
- if (!themeName || [themeName isEqualToString:@""]) {
- return nil;
- }
- NSString *themePath=[Bundle_Path_Of_ThemeResource stringByAppendingPathComponent:themeName];
- if (dirExistsAtPath(themePath)) {
- self.themePath=themePath;
- //update to db
- [ConfigItemDao set:[NSMutableDictionary dictionaryWithObjects:@[@"主题设置",themeName]
- forKeys:@[@"itemKey",@"itemValue"]]];
- //init again
- [self initCurrentTheme];
- }
- return themeName;
- }
这是一个简单的ThemeManager,通常有可能会有更多的配置,比如: themedFontWithName:(NSString *)fontName 等,当然方式都是一样的。
- - (void)configUIAppearance{
- NSLog(@"base config ui ");
- }
该类默认没有实现,主要用于供sub class去 override。
- - (void)configUIAppearance{
- self.appNameLbl.strokeColor=[[ThemeManager sharedInstance] themeColor];
- [self.commentBtn setBackgroundImage:[[ThemeManager sharedInstance] themedImageWithName:@"aboutBtnBG.png"]
- forState:UIControlStateNormal];
- [self.shareBtn setBackgroundImage:[[ThemeManager sharedInstance] themedImageWithName:@"aboutBtnBG.png"]
- forState:UIControlStateNormal];
- [self.developerBtn setBackgroundImage:[[ThemeManager sharedInstance] themedImageWithName:@"aboutBtnBG.png"] forState:UIControlStateNormal];
- [super configUIAppearance];
- }
可以看到,所有需要使用主题的UI控件的相关设置,都抽取出来放到了configUIAppearance中。
该方法,在所有子controller中都不会显式调用,因为在BaseController的viewDidLoad方法中以及调用了。这样,如果子类override了它,就会调用子类的,如果没有override,则调用BaseController的默认实现。
- - (void)registerThemeChangedNotification{
- [Default_Notification_Center addObserver:self
- selector:@selector(handleThemeChangedNotification:)
- name:Notification_For_ThemeChanged
- object:nil];
- }
同时给出消息处理的逻辑:
- - (void)handleThemeChangedNotification:(NSNotification*)notification{
- UIImage *navBarBackgroundImg=[[[ThemeManager sharedInstance] themedImageWithName:@"themeColor.png"]
- resizableImageWithCapInsets:UIEdgeInsetsMake(0.0f, 0.0f, 1.0f, 1.0f)
- resizingMode:UIImageResizingModeTile];
- [self.navigationController.navigationBar setBackgroundImage:navBarBackgroundImg
- forBarMetrics:UIBarMetricsDefault];
- <pre name="code" class="cpp"> [self configUIAppearance];</pre>}
可以看到这里又调用了configUIAppearance,由于继承关系,这里的self指针指向的并非BaseController实例(而是sub controller的实例),所以调用的也是sub controller的方法。所以,虽然可能还停留在“主题设置的界面”,但其他已存活的controller已经接受到了切换主题的Notification,并悄悄完成了切换。
- - (void)themeButton_touchUpIndise:(id)sender{
- //unselect
- UIButton *lastSelectedBtn=(UIButton*)[self.view viewWithTag:(Tag_Start_Index + self.currentSelectedIndex)];
- lastSelectedBtn.selected=NO;
- //select
- UIButton *selectedBtn=(UIButton*)sender;
- self.currentSelectedIndex=selectedBtn.tag - Tag_Start_Index;
- selectedBtn.selected=YES;
- [[ThemeManager sharedInstance] changeTheme:((NSDictionary*)self.themeArr[self.currentSelectedIndex]).allKeys[0]];
- //post themechanged notification
- [Default_Notification_Center postNotificationName:Notification_For_ThemeChanged
- object:nil];
- }
大致的流程就是这样~
iOS开发之主题皮肤的更多相关文章
- iOS 开发设计常用软件及工具整理
1, xCode 2, AppCode 3, Skech 原型设计软件 4, Hype 动画设计工具 5, fontawsome 免费图表 6, Prepo icon, images.catlog 生 ...
- IOS开发中设置导航栏主题
/** * 系统在第一次使用这个类的时候调用(1个类只会调用一次) */ + (void)initialize { // 设置导航栏主题 UINavigationBar *navBar = [UINa ...
- 【转】几点 iOS 开发技巧
[译] 几点 iOS 开发技巧 原文:iOS Programming Architecture and Design Guidelines 原文来自破船的分享 原文作者是开发界中知晓度相当高的 Mug ...
- 几点iOS开发技巧
转自I'm Allen的博客 原文:iOS Programming Architecture and Design Guidelines 原文来自破船的分享 原文作者是开发界中知晓度相当高 ...
- iOS开发之再探多线程编程:Grand Central Dispatch详解
Swift3.0相关代码已在github上更新.之前关于iOS开发多线程的内容发布过一篇博客,其中介绍了NSThread.操作队列以及GCD,介绍的不够深入.今天就以GCD为主题来全面的总结一下GCD ...
- iOS开发系列--App扩展开发
概述 从iOS 8 开始Apple引入了扩展(Extension)用于增强系统应用服务和应用之间的交互.它的出现让自定义键盘.系统分享集成等这些依靠系统服务的开发变成了可能.WWDC 2016上众多更 ...
- iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总
--系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用 ...
- iOS开发之窥探UICollectionViewController(五) --一款炫酷的图片浏览组件
本篇博客应该算的上CollectionView的高级应用了,从iOS开发之窥探UICollectionViewController(一)到今天的(五),可谓是由浅入深的窥探了一下UICollectio ...
- iOS开发之窥探UICollectionViewController(四) --一款功能强大的自定义瀑布流
在上一篇博客中<iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流>,自定义瀑布流的列数,Cell的外边距,C ...
随机推荐
- JQUERY选择和操作DOM元素(利用正则表达式的方法匹配字符串中的一部分)
JQUERY选择和操作DOM元素(利用正则表达式的方法匹配字符串中的一部分) 1.匹配属性的开头 $("[attributeName^='value']"); 2.匹配属性的结尾 ...
- react native调试
进入安卓终端 /usr/local/android-sdk-linux/platform-tools/adb shell 网络错误,模拟器不能连接主机,主要问题有2个: 移动端网络设置错误 服务没有启 ...
- jmake 编译当前目录c/c++单文件 指定文件 可加选项
基础版本的jmake是将所有当前文件夹下的C/C++文件生成单文件编译命令,并且jmake命令不可加选项. 现在做的改进是能在输入命令jmake时加上一些选项了,‘-’开头的选项加入到每个编译单文件的 ...
- SQL Server 文件流文件组
背景: 文件流通过在文件系统上存储blob数据文件将数据库引擎与ntfs文件集成在一起,使用t-sql和win32访问数据. 文件流使用windows系统来缓存数据,有助于在减少文件流数据对sql s ...
- network-manager与interfaces冲突
网络配置的两种方式 Ubuntu下修改网络配置有两种方式:图形界面方式(network-manager)和修改/etc/network/interfaces 但是如果两种方式的网络设置不同,就会产生冲 ...
- Delphi中的消息截获(六种方法:Hook,SubClass,Override WndProc,Message Handler,RTTI,Form1.WindowProc:=@myfun)good
Windows是一个基于消息驱动的系统,因此,在很多时候,我们需要截获一些消息然后自己进行处理.而VCL系统又有一些特定的消息.下面对我所了解的delphi环境中截获消息进行一些总结. 就个 ...
- 如何在你的project中使用support library【转】
Android support library是google以jar包形式提供的一个代码库,里面包含一些向后兼容的framework API以及一些只有在这个library中才提供的feature. ...
- poj3650---将一个字符串中的特定字符转换
#include <stdio.h> #include <stdlib.h> #include<string.h> int main() { ]; int i; w ...
- 面试题25:最小的K个数
方法一:对n个整数进行排序(快速排序或堆排序),取出前K个元素(最容易想到的最笨的方法,不可取) 时间复杂度:O(n*logn) + O(k) = O(n*logn) 采用快速排序的代码: #incl ...
- 10个Laravel4开发者必用扩展包
Laravel是一个新的基于最新PHP版本号语法,支持IoC等设计模式的高速开发框架.眼下最新版本号为4.2,推荐安装PHP版本号5.5+. 本文列举10个基本软件包,都是开发人员使用Laravel框 ...