主题切换,其实就是更改所有界面的UI图,比如tabBar上的,NavigationBar上的,还有viewController的背景图等等。
但是这些图片放在哪儿呢?
联想一下,平时我们安装程序的时候有这个主题切换吗?
一般是没有的,除非一些非常二的程序才有。
原因是什么呢?
因为这些主题占用内存太大,直接放到程序里,你的程序会非常大。这是主要原因。
废话不多说了,开始进入正题。
1,首先准备好你的图片。
2,将功能拖入程序中。(实际中只要下载下来放到手机一个特定的文件里就行)
3,重点来了,在拖入程序时,有几个选项,Copy if need 这项肯定是要勾选的,不勾会出现什么情况呢?如果你不勾选的,你在项目中删除的时候,有可能会导致源文件一起删了。 上图吧,不打了,看着都是勾选哪些。
4,点击finish之后,看看你的那个文件夹得颜色是否是绿色的,如果不是,说明你得重新弄了
下面就是代码时刻了。好激动啊,又可以装逼了。
#import "ViewController.h"
@interface ViewController ()
{
BOOL isLight;
NSString *lightName;
NSString *darkName;
}
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//获取应用程序包的路径
NSString *filePath = [[NSBundle mainBundle] resourcePath];
NSString *path1 = [filePath stringByAppendingString:@"/image/image1/11.jpg"];
NSString *path2 = [filePath stringByAppendingString:@"/image/image2/11.jpg"];
lightName = path1;
darkName = path2;
_imageV.image = [UIImage imageWithContentsOfFile:path1];
isLight = YES;
[self leftButton];
[self loadImageName];
}
- (void)loadImageName{
//存放主题文件名
NSMutableArray *fileArray = [[NSMutableArray alloc] init];
//读取plist文件
NSString *topPath = [[NSBundle mainBundle] pathForResource:@"theme" ofType:@"plist"];
NSDictionary *dicPath = [NSDictionary dictionaryWithContentsOfFile:topPath];
for (NSString *key in dicPath.allKeys) {
NSString *fileName = [dicPath objectForKey:key];
[fileArray addObject:fileName];
}
}
// NSFileManager *manage = [NSFileManager defaultManager];
拼写app内部文件的实际路径
// NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"图片/Skins"];
// //获取文件夹下所有子文件的路径
// NSArray *subFilePath = [manage subpathsAtPath:filePath];
//
// NSLog(@"%@",subFilePath);
- (void)leftButton{
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"change" style:UIBarButtonItemStyleDone target:self action:@selector(barButtonItemAction:)];
}
- (void)barButtonItemAction:(UIBarButtonItem *)button{
NSLog(@"---");
isLight = !isLight;
if (isLight == NO) {
_imageV.image = [UIImage imageWithContentsOfFile:darkName];
}else{
_imageV.image = [UIImage imageWithContentsOfFile:lightName];
}
}
以上只是一个简单的小demo,复杂的工程,有复杂的功能需求,不积跬步无以至千里,希望对各位能有所帮助。