<h1>我们要先知道</h1>
<h4>1.皮肤是什么? 图片
2.换肤是什么 ? 改变图片
3.通过什么方式改变? 管理类加上通知的方法
4.改变那些图片?tabbar,navigation,我的,以及页面的背景。
5.换肤应用在那里? (1)节假日切换主题(2)切换夜晚模式(3)大主题
6.必要条件是什么? (1)不同主题的同一位置图片名称必须相同(2) 我们的所有controller都继承于一个BaseViewController。BaseViewController里的方法</h4>(这里只以navigation和tarbar人为例了。)
<pre>
-
(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];
}
</pre>
在初始化BaseViewController的方法里加上通知
<pre> -
(id) init {
if (self == [super init]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeChangedNotfication:) name:@"通知名" object:nil];
}[self reloadThemeImage];
return self;
}
//通知方法 -
(void) themeChangedNotfication:(NSNotification *)notification {
[self reloadThemeImage];
}
</pre>
点击换主题地方代码
<pre>
-
(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];
}
</pre>
<h2>管理类</h2>
.h文件
<pre>
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
</pre>
.m文件
<pre>
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
</pre>
theme.plist是什么
资源目录结构
64229767-8F68-45A9-A948-47928B74F105.png
其他背景统一封装在baseviewcontroller中用。无法封装的背景只能另接收通知处理。当然是肯定可以封装的。hiden方法控制在唯一界面显示。有更好的方法欢迎指点。
网友评论