正常项目加载storyboard、xib、图片等资源时候,这些资源都是在mainBundle里面,所以直接通过系统提供的方法即可获取到,但是在组件中,每个组件中有自己的bundle,组件的storyboard、xib、图片等资源都是在自己的bundle里面,所以在组件中想使用自己的资源时候就需要先获取到自己的bundle,然后通过bundle加载的方式加载对应的资源
另外在组件中的storyboard和xib中使用图片时候直接在对应控件中输入图片名就会自己提升填充的
获取bundle的分类,其他分类依赖bundle分类
#import "NSBundle+test.h"
@implementation NSBundle (test)
/**
通过bundle名获取对应的bundle
@param bundleStr bundle名
@return 对应的bundle
*/
+ (NSBundle *)by_myLibraryBundle:(NSString *)bundleStr targetClass:(Class)targetClass {
NSURL *url = [self by_myLibraryBundleURL:bundleStr targetClass:targetClass];
return [self bundleWithURL:url];
}
+ (NSURL *)by_myLibraryBundleURL:(NSString *)bundleStr targetClass:(Class)targetClass{
NSBundle *bundle = [NSBundle bundleForClass:targetClass];
return [bundle URLForResource:bundleStr withExtension:@"bundle"];
}
使用组件里图片的分类
#import "UIImage+test.h"
@implementation UIImage (test)
/**
加载组件中的图片
@param imageName 图片名,组件中需要有2x和3x的图片
@param bundle 对应组件的bundle
@param targetClass 对应组件中的任一类
@return 获取到的图片
*/
+(UIImage *)loadBundleImage:(NSString *)imageName bundle:(NSString *)bundle targetClass:(Class)targetClass{
NSInteger scale = [[UIScreen mainScreen] scale];
NSBundle *currentBundle = [NSBundle bundleForClass:targetClass];
NSString *name = [NSString stringWithFormat:@"%@@%zdx",imageName,scale];
NSString *dir = [NSString stringWithFormat:@"%@.bundle",bundle];
NSString *path = [currentBundle pathForResource:name ofType:@"png" inDirectory:dir];
return path ? [UIImage imageWithContentsOfFile:path] : nil;
}
@end
从组件中获取xib中的view
#import "UIView+TEST.h"
#import "NSBundle+test.h"
@implementation UIView (TEST)
/**
获取bundle里面的xib对应的view
@param bundleStr 对应的bundle
@return 获取到的对象view
*/
+(instancetype)loadFrameBundle:(NSString *)bundleStr{
NSBundle *xibBundle = [NSBundle by_myLibraryBundle:bundleStr targetClass:self];
return [[xibBundle loadNibNamed:NSStringFromClass(self) owner:nil options:nil] firstObject];
}
/**
获取bundle里面的xib对应的view
@param frame view的frame
@param bundleStr 对应的bundle
@return 对象view
*/
+(instancetype)loadFrameBundleWithFrame:(CGRect)frame bundleStr:(NSString *)bundleStr{
UIView *view = [self loadFrameBundle:bundleStr];
view.frame = frame;
return view;
}
@end
从组件中的storyboard中获取对应的控制器
#import "UIViewController+test.h"
#import "NSBundle+test.h"
@implementation UIViewController (test)
/**
获取组件对应的控制器,默认storyboard中控制器对应的identifier是控制的类名
@param bundleStr 对应组件的bundle
@param storyboardName 对应storyboard名
@return 获取到的控制器
*/
+(instancetype)loadFrameBundle:(NSString *)bundleStr storyboardName:(NSString *)storyboardName{
;
return [self loadFrameBundle:bundleStr storyboardName:storyboardName identifier:NSStringFromClass(self)];
}
/**
获取组件对应的控制器
@param bundleStr 对应组件的bundle
@param storyboardName 对应storyboard名
@param identifier storyboard中控制器对应的identifier
@return 取到的控制器
*/
+(instancetype)loadFrameBundle:(NSString *)bundleStr storyboardName:(NSString *)storyboardName identifier:(NSString *)identifier{
NSBundle *bundle = [NSBundle by_myLibraryBundle:bundleStr targetClass:self];
UIViewController *vc = [[UIStoryboard storyboardWithName:storyboardName bundle:bundle] instantiateViewControllerWithIdentifier:identifier];
return vc;
}
@end