動態(tài)庫形式:.dylib和.framework
靜態(tài)庫形式:.a和.framework
舉例demo:demoStatic
1、動態(tài)庫情況xib資源存在路徑
iOS在組件化過程中,一般來說,會把組件做成動態(tài)庫的形式,也就是使用use_frameworks!

圖1

圖2
那么app運行起來之后,當app需要加載xib資源,會從
demoStatic.framework這個動態(tài)庫中尋找,如果xib資源放在了Assets文件夾下,那么系統(tǒng)應(yīng)該從demoStatic.bundle里面找xib資源;如果xib資源放在了Classes文件夾下,那么系統(tǒng)應(yīng)該直接從demoStatic.framework里面找(即demoStatic.bundle同級目錄)。下圖為
demoStatic_Example.app的包內(nèi)容(靜態(tài)庫和動態(tài)庫兩種情況,此時的xib資源都在Assets文件夾下,所以xib的資源都應(yīng)該在demoStatic.bundle里面找,只是不同情況demoStatic.bundle的位置不同)
圖3|動態(tài)庫|Assets

圖3|靜態(tài)庫|Assets

圖3|動態(tài)庫|Classes
podspec文件中聲明s.static_framework = true,如下圖
圖4
demoStatic組件允許打包成靜態(tài)庫,這樣的話,demoStatic_Example.app包內(nèi)容的Frameworks路徑下就沒有demoStatic.framework,所以這種情況下,必須把xib資源放到Assets文件夾下(參考圖3|靜態(tài)庫)[因為靜態(tài)庫的組件中,它的bundle路徑在demoStatic_Example.app下]。為了和圖片資源區(qū)分清楚,盡量在podSpec文件下指明路徑,參考圖4的s.source_bundles下面我寫了一個加載Assets文件夾下xib資源的方法
#import "NSBundle+Xib.h"
@implementation NSBundle (Xib)
///這是NSBundle的Category
/// 如果xib文件放到了Assets文件夾下使用這個方法獲取xib所在bundle(兼容靜態(tài)庫和動態(tài)庫)
/// @param bundleName bundle名稱
+ (instancetype)yl_xibWithBundle:(NSString *)bundleName {
if ([bundleName containsString:@".bundle"]) {
bundleName = [bundleName componentsSeparatedByString:@".bundle"].firstObject;
}
NSBundle *lastBundle = nil;
//沒使用framwork的情況下(靜態(tài)庫)
NSURL *associateBundleURL = [[NSBundle mainBundle] URLForResource:bundleName withExtension:@"bundle"];
//使用framework形式(動態(tài)庫)
if (!associateBundleURL) {
associateBundleURL = [[NSBundle mainBundle] URLForResource:@"Frameworks" withExtension:nil];
associateBundleURL = [associateBundleURL URLByAppendingPathComponent:bundleName];
associateBundleURL = [associateBundleURL URLByAppendingPathExtension:@"framework"];
associateBundleURL = [associateBundleURL URLByAppendingPathComponent:bundleName];
associateBundleURL = [associateBundleURL URLByAppendingPathExtension:@"bundle"];
}
lastBundle = [NSBundle bundleWithURL:associateBundleURL];
return lastBundle;
}
@end