How do I load a xib file from my cocoapod?

对着背影说爱祢 提交于 2019-12-05 01:34:48

The s.resources folder will not automatically recurse to subdirectories. I had to specify the full relative path to the xib files.

The issue is that your resource file is not being included in the mainBundle you should look at some of the NSBundle docs and use something like bundleForClass: to get the correct bundle to load the xib from. The output of [NSBundle allBundles may also be informative.

You can use s.resources with in your library podspec. Add s.resources = 'FOLDER_INCLUDES_XIB_FILES/*.xib'. And to initiate your class with xib file

Objective-C

NSBundle *bundle = [NSBundle bundleForClass:self.classForCoder];
UINib *nib = [[bundle loadNibNamed:@"YOUR_XIB_FILE_NAME" owner:nil options:nil] firstObject];

Swift 3 and Swift 4

let bundle = Bundle(for: self.classForCoder())
return bundle.loadNibNamed("YOUR_XIB_FILE_NAME", owner: nil, options: nil)?.first

you need to all all nibs in {podName}.podspec

s.resource_bundles = {
'{podName}' => ['{podName}/Classes/*.xib']

}

and when you need to declare new view

NSBundle *podBundle = [NSBundle bundleForClass:[viewController class]];
id data = [podBundle URLForResource:@"{podName}" withExtension:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithURL:data];
viewController *view = [[viewController alloc]initWithNibName:@"viewController" bundle:bundle];

replace {podName} to your project name

I run to this issue,

and refer to Keith Smiley's answer, I go to docs have a look:

I finnally change the bundle init method from Class like this:

NSBundle *bundle = [NSBundle bundleForClass:[EBBannerView class]];
NSArray *banners = [bundle loadNibNamed:@"EBBannerView" owner:nil options:nil];

Then it works fine.

In here you need to add this into your pod spec.

 s.resources = "YourProjectName/*.xib"
 s.resource_bundles = {
   'YourProjectName' => [
       'Pod/**/*.xib'
   ]
 }

Then retrieve the xib file from the bundle like this:-

 let podBundle = Bundle(for:YourClassName.self)
    if let bundleURL = podBundle.url(forResource: "YourProjectName", withExtension: "bundle") {
        if let bundle = Bundle(url: bundleURL) {
            let cellNib = UINib(nibName: "nibFileName", bundle: bundle)
            navigationTableView.register(cellNib, forCellReuseIdentifier: "nibFileReuseIdentifierName")
        } else {
            assertionFailure("Could not load the bundle")
        }
    } else {
        assertionFailure("Could not create a path to the bundle")
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!