How to add xib file to static library in iOS

I am trying to add an xib file or any other view controller file to a static library, but I cannot do this. Can you help me?

If possible, add all source code.

their button is in the first view. And when you click on this button, the new view controllers come up with something (say, background color changes). How to create a static library for this? so can i use it in another project?

Thank you in advance

+6
source share
3 answers

Strictly speaking, it is correct that you cannot add Xib to a static library. However, there are workarounds. Matt Galloway iOS Resource Library Helps You Show How To Do It.

See iOS library with resources .

+4
source

If you want to create your own interfaces using the interface designer when creating a static library, you will need to create a package and distribute it with your library.

In Xcode:

  • File> New> Goal
  • Select "Bundle" from the OS X Framework and Library section.
  • Fill in the data. The package structure should be the basis.

Then you need to build your package at the same time as your framework. Add the node to the Target Dependencies assembly phase of your structure.

When you make your xibs, you make your goal with this new package that you created.

Then, when you compile your framework in the derived data directory, next to your binary, you will find your compiled package. You pass this on to third parties along with the binary framework.

So how do you reference this package in your code? On iOS, packages cannot be downloaded, and your package will indeed be in a third-party iOS application. You can create a category in NSBundle for convenient access to your collection from your code:

@interface NSBundle (mybundle) +(NSBundle *)myBundle; @end @implementation NSBundle (mybundle) static NSBundle * _myBundle; +(NSBundle *)myBundle{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSBundle * mainBundle = [NSBundle mainBundle]; NSString * pathToMyBundle = [mainBundle pathForResource:@"myBundle" ofType:@"bundle"]; NSAssert(pathToMyBundle, @"bundle not found", nil); _myBundle = [NSBundle bundleWithPath:pathToMyBundle]; }); return _myBundle; } 

You can then access your package in the xibs download code as follows:

 UIViewController * controller = [[UIViewController alloc] initWithNibName:nil bundle:[NSBundle myBundle]]; 

Remember that if you use categories in the framework code, you will need to make sure that your infrastructure users add -ObjC (or -all_load if not using recent Xcode) "another linker flag" to their projects

+6
source

XIB is like a resource, and which static library is a compiled file containing ur classes. You can say that your finished code is ready to use. You cannot add xib to a static library.

0
source

Source: https://habr.com/ru/post/949120/


All Articles