我的Pod框架中有一个CalendarCell.xib和一个CalendarCell.swift UIView子类。 CalendarCell.swift具有连接到其视图的出口。在运行时我收到此错误:
无法在捆绑中加载NIB:' NSBundle(已加载)'名称' CalendarCell'
在我的podspec文件中,我创建了一个包资源:
s.resource_bundles = { 'Resources' => ['MyFramework/**/*.{xib,xcassets}'] }
如果我安装了我的Pod,我可以看到xib文件,但它崩溃了,现在看来xib文件和它的swift对应物都在不同的包中。
任何帮助将不胜感激!
答案 0 :(得分:8)
你是对的,xib存储在另一个包中。您需要找到xibs包的路径并从该包中加载所有xib,而不是从主包中加载它们。
要在Swift 3中找到捆绑包的路径:
UINib
因此,您必须从该捆绑包中加载let nib = UINib(nibName: "YourXibName", bundle: bundle)
:
UITableView
如果您出于此目的需要,请将其注册到UICollectionView
/ self.tableView.register(nib, forCellReuseIdentifier: "cellIdentifier")
:
OrderBy
答案 1 :(得分:0)
这就是我正在迅速使用的方法,它也适用于主要包和框架
extension UINib {
func instantiate() -> Any? {
return self.instantiate(withOwner: nil, options: nil).first
}
}
extension UIView {
static var nib: UINib {
return UINib(nibName: String(describing: self), bundle: Bundle.init(for: self))
}
static func instantiate(autolayout: Bool = true) -> Self {
// generic helper function
func instantiateUsingNib<T: UIView>(autolayout: Bool) -> T {
let view = self.nib.instantiate() as! T
view.translatesAutoresizingMaskIntoConstraints = !autolayout
view.autoresizingMask = []
return view
}
return instantiateUsingNib(autolayout: autolayout)
}
}
要使用init视图
let myView = MyView.instantiate()