CharlieOxendine
CharlieOxendine

Reputation: 61

Does Swift Package not support Xib files

I am currently trying to add a xib file for a custom popup to my Swift Package. However, it won't says:

'Could not load NIB in bundle: 'NSBundle </private/var/containers/Bundle/Application/--/testingSDK.app> (loaded)' with name 'consentViewModal''

I have seen around people discussing that Swift Packages doesn't support anything but source code and I was wondering if this was still true. Below is the code that we are using to instantiate the custom view.

let nib = UINib(nibName: "consentViewModal", bundle: Bundle(for: consentViewModal.classForCoder()))

guard let view = nib.instantiate(withOwner: self, options: nil).first as? UIView else {
    fatalError("Failed to instantiate nib \(nib)")
}
    
self.addSubview(view)
view.frame = self.bounds
view.autoresizingMask = [.flexibleHeight, .flexibleWidth]

Upvotes: 0

Views: 1048

Answers (1)

cohen72
cohen72

Reputation: 2988

  1. When the .xib is inside of a Swift Package, you must use Bundle.module

    The "module" bundle "Returns the resource bundle associated with the current Swift module."

    For example:

    let loadedNib = Bundle.module.loadNibNamed(String(describing: type(of: self)), owner: self, options: nil)
    
    guard let contentView = loadedNib?.first as? UIView else {
       return nil
    }
    

  1. Also, ensure that within the .xib itself, that you update the "module" name to be that of your Swift Package on the right hand side "Identity Inspector". It should not say "None" as shown below:

    enter image description here


3. Also, although I'm not sure if necessary, place the `.xib` file within the "Resources" folder under the root directory of your target folder.

Upvotes: 2

Related Questions