维护一套同时兼容 iOS 6 和 iOS 7,并且能够自动适应两个系统的 UI 风格的代码
2013-09-25 11:30
387 查看
举例:
ios6:test.png
ios7:ios7_test.png
在ios7Image.plist中添加 "test"
PS:如果要统一成ios7风格,可以看看UI7Kit youknowone/UI7Kit 路 GitHub
@implementation UIImage (HT)
static NSArray *ios7Images;
+ (void)load {
if (IOS7) {
static dispatch_once_t once;
dispatch_once( &once, ^{
ios7Images = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"iOS7Images" ofType:@"plist"]];
[NSObject swizzleClassMethod:self from:@selector(imageNamed:) to:@selector(custom_imageNamed:)];
});
}
}
+ (UIImage *)custom_imageNamed:(NSString *)imageName {
if ([ios7Images containsObject:imageName]) {
return [self custom_imageNamed:[NSString stringWithFormat:@"ios7_%@",imageName]];
}
return [self custom_imageNamed:imageName];
}
@end
OK,图片多的时候怎么办呢?设计师弄好全部以ios7_开头命名的图片,放到一个目录,然后代码
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [searchPaths objectAtIndex:0];
NSString *path = [documentPath stringByAppendingString:@"/images"];
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
NSMutableArray *array = [NSMutableArray array];
for (NSString *file in files) {
if ([file isEqualToString:@".DS_Store"]) {
continue;
}
NSString *temp = [file stringByReplacingOccurrencesOfString:@"ios7_" withString:@""];
temp = [temp stringByReplacingOccurrencesOfString:@"@2x" withString:@""];
temp = [temp stringByReplacingOccurrencesOfString:@".png" withString:@""];
if ([array containsObject:temp]) {
continue;
}
[array addObject:temp];
}
NSString *plistPath = [documentPath stringByAppendingString:@"/iOS7Images.plist"];
[array writeToFile:plistPath atomically:YES];
ios6:test.png
ios7:ios7_test.png
在ios7Image.plist中添加 "test"
PS:如果要统一成ios7风格,可以看看UI7Kit youknowone/UI7Kit 路 GitHub
@implementation UIImage (HT)
static NSArray *ios7Images;
+ (void)load {
if (IOS7) {
static dispatch_once_t once;
dispatch_once( &once, ^{
ios7Images = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"iOS7Images" ofType:@"plist"]];
[NSObject swizzleClassMethod:self from:@selector(imageNamed:) to:@selector(custom_imageNamed:)];
});
}
}
+ (UIImage *)custom_imageNamed:(NSString *)imageName {
if ([ios7Images containsObject:imageName]) {
return [self custom_imageNamed:[NSString stringWithFormat:@"ios7_%@",imageName]];
}
return [self custom_imageNamed:imageName];
}
@end
OK,图片多的时候怎么办呢?设计师弄好全部以ios7_开头命名的图片,放到一个目录,然后代码
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [searchPaths objectAtIndex:0];
NSString *path = [documentPath stringByAppendingString:@"/images"];
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
NSMutableArray *array = [NSMutableArray array];
for (NSString *file in files) {
if ([file isEqualToString:@".DS_Store"]) {
continue;
}
NSString *temp = [file stringByReplacingOccurrencesOfString:@"ios7_" withString:@""];
temp = [temp stringByReplacingOccurrencesOfString:@"@2x" withString:@""];
temp = [temp stringByReplacingOccurrencesOfString:@".png" withString:@""];
if ([array containsObject:temp]) {
continue;
}
[array addObject:temp];
}
NSString *plistPath = [documentPath stringByAppendingString:@"/iOS7Images.plist"];
[array writeToFile:plistPath atomically:YES];
相关文章推荐
- 用于Matlab的多摄像机系统校准工具箱 用于Matlab的多摄像机系统校准工具箱 这是用于校准多摄像机系统的工具箱。该工具箱的要求是系统中的两个相邻摄像机应该能够同时看到校准板的某些部分。一般来说
- iOS 利用运行时交换系统方法实现禁止同时点击两个按钮触发多个事件
- iOS 一套@3x图片兼容所有iOS设备和系统
- iOS中Cell高度如何能够自动适应需要显示的内容
- 更改IntelliJ的快捷键为Eclipse风格,并且恢复Alt+/代码自动补全快捷键code completion
- c++继承应用->写一套代码跨越IOS和Android两个平台
- IE6中,一个Button同时打开两个下载窗口,并且可以自动关闭
- iOS UIViewController 无法关闭自动旋屏(自动旋转、手动旋转、兼容IOS6之前系统)
- 论坛源码推荐(2月28日):Xcode 5代码自动补全插件 iOS 7风格的导航栏按钮
- 更改Intellij的快捷键为Eclipse风格,并且恢复Alt+/代码自动补全快捷键code completion
- 更改IntelliJ的快捷键为Eclipse风格,并且恢复Alt+/代码自动补全快捷键code completion
- iOS中Cell高度如何能够自动适应需要显示的内容
- Xcode 5代码自动补全插件 iOS 7风格的导航栏按钮
- 北斗系统与GPS实现兼容 用户可同时使用两个系统
- iOS中Cell高度如何能够自动适应需要显示的内容
- ECshop两个自动收缩浮动在线客服代码,QQ客服、在线MSN、旺旺,兼容IE、 FF
- IOS屏幕旋转详解(自动旋转、手动旋转、兼容IOS6之前系统)
- iOS 快速创建常用UI,使代码更加整洁并且提高开发效率<1>
- iOS 一套@3x图片兼容所有iOS设备和系统
- ECshop两个自动收缩浮动在线客服代码,QQ客服、在线MSN、旺旺,兼容IE、 FF