An easy way to change theme through NSObject's category for iOS.
NSObject+PYThemeExtension
pod "PYTheme"
#import
PYTheme文件夹中的所有文件拽入项目中
#import "NSObject+PYThemeExtension.h"
// 按钮的背景色添加到主题色池中 [button py_addToThemeColorPool:@"backgroundColor"];
// 设置按钮选中时字体颜色添加到主题色池中 [button py_addToThemeColorPoolWithSelector:@selector(setTitleColor:forState:) objects:@[PYTHEME_THEME_COLOR, @(UIControlStateSelected)]];
// 设置tabBar选中时字体颜色添加到主题色池中 NSMutableDictionary *attrSel = [NSMutableDictionary dictionary]; attrSel[NSForegroundColorAttributeName] = PYTHEME_THEME_COLOR; [childController.tabBarItem py_addToThemeColorPoolWithSelector:@selector(setTitleTextAttributes:forState:) objects:@[attrSel, @(UIControlStateSelected)]];
// 设置主题色为红色 [self py_setThemeColor:[UIColor redColor]];
// 添加tabBarItem到主题图片池中 [tabBarItem py_addToThemeImagePool];
// 重新加载主题图片,并设置主题色为红色 [self py_reloadThemeImageWithThemeColor:[UIColor redColor] setting:^(const NSArray *objects) { // 根据控件类型完成相关设置 }
UIAppearance,否则将无法添加到主题池中 如下:将导致navigationBar无法添加到主题色池中
// 获取全局navBar UINavigationBar *navBar = [UINavigationBar appearance]; // 添加背景色到主题色池中 [navBar py_addToThemeColorPool:@"barTintColor"];
使用
py_addToThemeColorPoolWithSelector:objects:设置参数数组时注意:
如果要使图片随着主题色变化而渲染成主题颜色,需要设置
RenderingMode为:UIImageRenderingModeAlwaysTemplate
All source code is licensed under the MIT License.