作用:在不同日子显示不同风格界面;老板要改已经完成的app的主题颜色;这里不是实现本地更换主题;
1、创建主题管理器
在.h文件中:
@property (nonatomic, copy) NSString * themeName; // 主题名字
@property (nonatomic, retain) NSDictionary * themePlistDict; // 主题属性列表字典
+ (ThemeManager *) sharedThemeManager;
- (UIImage *) themeImageWithName:(NSString *)imageName;
在.m文件里:
+ (ThemeManager *) sharedThemeManager{
static ThemeManager *theme = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
theme = [[ThemeManager alloc] init];
});
return theme;
}
-(instancetype)init{
if (self = [super init]) {
NSString * themePath = [[NSBundle mainBundle] pathForResource:@"theme" ofType:@"plist"];
self.themePlistDict = [NSDictionary dictionaryWithContentsOfFile:themePath];
self.themeName = nil;
}
return self;
}
- (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];
NSString * themeFilePath = [resourcePath stringByAppendingPathComponent:themeSubPath];
return themeFilePath;
}