正常項(xiàng)目加載storyboard、xib、圖片等資源時候,這些資源都是在mainBundle里面,所以直接通過系統(tǒng)提供的方法即可獲取到,但是在組件中,每個組件中有自己的bundle,組件的storyboard、xib、圖片等資源都是在自己的bundle里面,所以在組件中想使用自己的資源時候就需要先獲取到自己的bundle,然后通過bundle加載的方式加載對應(yīng)的資源
另外在組件中的storyboard和xib中使用圖片時候直接在對應(yīng)控件中輸入圖片名就會自己提升填充的
獲取bundle的分類,其他分類依賴bundle分類
#import "NSBundle+test.h"
@implementation NSBundle (test)
/**
通過bundle名獲取對應(yīng)的bundle
@param bundleStr bundle名
@return 對應(yīng)的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 對應(yīng)組件的bundle
@param targetClass 對應(yīng)組件中的任一類
@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對應(yīng)的view
@param bundleStr 對應(yīng)的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對應(yīng)的view
@param frame view的frame
@param bundleStr 對應(yīng)的bundle
@return 對象view
*/
+(instancetype)loadFrameBundleWithFrame:(CGRect)frame bundleStr:(NSString *)bundleStr{
UIView *view = [self loadFrameBundle:bundleStr];
view.frame = frame;
return view;
}
@end
從組件中的storyboard中獲取對應(yīng)的控制器
#import "UIViewController+test.h"
#import "NSBundle+test.h"
@implementation UIViewController (test)
/**
獲取組件對應(yīng)的控制器,默認(rèn)storyboard中控制器對應(yīng)的identifier是控制的類名
@param bundleStr 對應(yīng)組件的bundle
@param storyboardName 對應(yīng)storyboard名
@return 獲取到的控制器
*/
+(instancetype)loadFrameBundle:(NSString *)bundleStr storyboardName:(NSString *)storyboardName{
;
return [self loadFrameBundle:bundleStr storyboardName:storyboardName identifier:NSStringFromClass(self)];
}
/**
獲取組件對應(yīng)的控制器
@param bundleStr 對應(yīng)組件的bundle
@param storyboardName 對應(yīng)storyboard名
@param identifier storyboard中控制器對應(yīng)的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