<h1>我們要先知道</h1>
<h4>1.皮膚是什么裆悄? 圖片
2.換膚是什么 ? 改變圖片
3.通過什么方式改變萎庭? 管理類加上通知的方法
4.改變那些圖片霜医?tabbar,navigation驳规,我的肴敛,以及頁面的背景。
5.換膚應(yīng)用在那里达舒? (1)節(jié)假日切換主題(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:@"默認(rèn)"]) {
themeName = nil;
}
// 記錄當(dāng)前主題名字
themeManager.themeName = themeName;
[[NSNotificationCenter defaultCenter] postNotificationName:kThemeChangedNotification object:nil];
// 主題持久化
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:themeName forKey:kThemeNameKey];
[userDefaults synchronize];
// 重新加載數(shù)據(jù)顯示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是什么
資源目錄結(jié)構(gòu)
其他背景統(tǒng)一封裝在baseviewcontroller中用。無法封裝的背景只能另接收通知處理趾代。當(dāng)然是肯定可以封裝的贯底。hiden方法控制在唯一界面顯示。有更好的方法歡迎指點撒强。