Reputation: 61
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
Reputation: 2988
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
}
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:
Upvotes: 2