组件化---私有库中的图片和xib

私有库中出现以下情况要注意了

1、加载图片:例如YHTestOne

1》场景:在UIViewController上用纯代码方式添加一个按钮

用[headBtn setImage:[UIImage imageNamed:@"Enterprise_bots_woyaotikuan"] forState:UIControlStateNormal]

给vc的button加载图片时可以的。

2》Enterprise_bots_woyaotikuan.png图片资源要放在与Classes同级的Assets文件夹下。

3》把本地组件工程库的podspec文件的

s.resource_bundles = {

       'YHTestThree' => ['YHTestThree/Assets/*.png']

   }文件打开

 

2、控制器创建时使用xib方式创建,例如YHTestThree

1》场景:在UIViewController上用纯代码方式添加两个按钮,一个直接在xib上给图片,一个按钮拉线给图片

2》将xib文件放在与Classes同级的Assets文件夹下

3》把本地组件工程库的podspec文件的s.resource_bundles改为

s.resource_bundles = {

       'YHTestThree' => ['YHTestThree/Assets/*']

   }

 这是xib写进私有库的一大坑

报错:

-> YHTestThree (0.0.1)

    - WARN  | url: The URL (https://gitee.com/denggun/YHTestThree) is not reachable.

    - NOTE  | xcodebuild:  note: Using new build system

    - NOTE  | [iOS] xcodebuild:  note: Planning build

    - NOTE  | [iOS] xcodebuild:  note: Constructing build description

    - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file. (in target 'App')

 

Analyzed 1 podspec.

解决:把下图红框的勾去掉即可,即去掉安全区域限制

pod spec lint --allow-warnings 报错

 -> YHTestThree (0.0.1)

    - WARN  | url: The URL (https://gitee.com/denggun/YHTestThree) is not reachable.

    - ERROR | [iOS] xcodebuild: Returned an unsuccessful exit code. You can use `--verbose` for more information.

    - NOTE  | xcodebuild:  note: Using new build system

    - NOTE  | [iOS] xcodebuild:  note: Planning build

    - NOTE  | [iOS] xcodebuild:  note: Constructing build description

    - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file. (in target 'App')

    - NOTE  | [iOS] xcodebuild:  YHTestThree/YHTestThree/Classes/YHTestThree.xib:Q5M-cg-NOt: error: Safe Area Layout Guide before iOS 9.0 [12]

解决:把podspec文件里的s.ios.deployment_target = '8.0' 改成s.ios.deployment_target = '9.0'即可

 

 YHTestThree *testThree = [[YHTestThree alloc] init];

    [self presentViewController:testThree animated:YES completion:nil];

    方式推出YHTestThree

报错:Warning: Attempt to present <YHTestThree: 0x7fd8e0f0f9a0> on <ViewController: 0x7fd8e0f0f3d0> whose view is not in the window hierarchy!

模拟器运行推出来的YHTestThree控制器是黑屏

现在私有库获取YHTestThree.xib不在mainBundle中了,需要到相应的bundle中找此xib。

通过path找bundle:

   NSBundle *bundle = [NSBundle bundleForClass:[YHTestThree class]];

    //在这个路径下找到子bundle的路径

    NSString *path = [bundle pathForResource:@"YHTestThree" ofType:@"bundle"];

    //根据路径拿到子bundle

    NSBundle *bundel = [NSBundle bundleWithPath:path];

    YHTestThree *cc =  [[YHTestThree alloc] initWithNibName:@"YHTestThree" bundle:bundel];

    [self presentViewController:cc animated:YES completion:nil];

按这种做法,运行正常

以上找xib所在bundle的方法,在我用自己的另一个私有库YHTestFour时,并不能正确找到xib的位置。

YHTestFour私有库里包含一个TableViewContraller,这个控制器里的cell是通过xib自定义的。

这个私有库的xib是这样找到:

 NSBundle *bundle = [NSBundle bundleForClass:NSClassFromString(@"YHTestFour")];

NSString *pathpath =  bundle.bundlePath;

NSString *newPath = [NSString stringWithFormat:@"%@/Frameworks/YHTestFour.framework/YHTestFour.bundle",pathpath];

NSBundle *bundel = [NSBundle bundleWithPath:newPath];

[self.tableView registerNib:[UINib nibWithNibName:@"YHTestFourCell" bundle:bundel] forCellReuseIdentifier:@"fourCell"]

这样工程就可以正常运行了。

其实YHTestFour这个找相应bundle的方法是最通用的,根据包的目录结构找到对应的budle路径,找到对应xib

3、如果私有库里有xib,则在用到此xib的工程的Podfile文件里要打开use_frameworks!,否则找不到此xib

注意:以私有库YHTestFour为例,如果YHTestFour已经上传传到远程仓库,工程用到此私有库产生崩溃,例如找不到YHTestFourCell,unlock此私有库并修改代码后直接运行,还是报错,这时候一定要卸载工程并clean工程后再运行。已经pod到工程的私有库,经常出现私有库代码已经修改正确了,但运行依旧报错误(可能是编译或缓存的问题),这时候切记要卸载并clean)

如果,卸载并clean工程后运行成功,要将修改后的代码复制到本地的私有代码工程中,将修改后的版本提交到远程私有库,再重新pod install到工程,再次运行,直到运行成功。


 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值