'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]
Solution 1:[1]
When the
.xibis inside of a Swift Package, you must useBundle.moduleThe "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 }
Also, ensure that within the
.xibitself, 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:
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

