无法从我自己的 Pod 加载 NIB
Could not load NIB from my own Pod
我的 Pod 框架中有一个 CalendarCell.xib 和一个 CalendarCell.swift UIView 子类。 CalendarCell.swift 有连接到其视图的插座。在运行时我得到这个错误:
Could not load NIB in bundle: 'NSBundle (loaded)' with name 'CalendarCell'
在我的 podspec 文件中,我创建了一个包资源:
s.resource_bundles = { 'Resources' => ['MyFramework/**/*.{xib,xcassets}'] }
如果我安装我的 Pod,我可以看到 xib 文件,但它崩溃了,现在 xib 文件和它的 swift 副本似乎在不同的包中。
如有任何帮助,我们将不胜感激!
没错,xib 存储在不同的包中。您需要找到 xibs 包的路径并从该包加载所有 xib,而不是从主包加载它们。
找到bundle的路径,在Swift3:
let bundle = Bundle(for: YourClass.self)
因此,您必须从该捆绑包中加载 UINib
:
let nib = UINib(nibName: "YourXibName", bundle: bundle)
并将其注册到您的 UITableView
/UICollectionView
,如果您需要它:
self.tableView.register(nib, forCellReuseIdentifier: "cellIdentifier")
这是我在 swift 中使用的,它也适用于主包和框架
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)
}
}
初始化视图使用
let myView = MyView.instantiate()
我的 Pod 框架中有一个 CalendarCell.xib 和一个 CalendarCell.swift UIView 子类。 CalendarCell.swift 有连接到其视图的插座。在运行时我得到这个错误:
Could not load NIB in bundle: 'NSBundle (loaded)' with name 'CalendarCell'
在我的 podspec 文件中,我创建了一个包资源:
s.resource_bundles = { 'Resources' => ['MyFramework/**/*.{xib,xcassets}'] }
如果我安装我的 Pod,我可以看到 xib 文件,但它崩溃了,现在 xib 文件和它的 swift 副本似乎在不同的包中。
如有任何帮助,我们将不胜感激!
没错,xib 存储在不同的包中。您需要找到 xibs 包的路径并从该包加载所有 xib,而不是从主包加载它们。
找到bundle的路径,在Swift3:
let bundle = Bundle(for: YourClass.self)
因此,您必须从该捆绑包中加载 UINib
:
let nib = UINib(nibName: "YourXibName", bundle: bundle)
并将其注册到您的 UITableView
/UICollectionView
,如果您需要它:
self.tableView.register(nib, forCellReuseIdentifier: "cellIdentifier")
这是我在 swift 中使用的,它也适用于主包和框架
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)
}
}
初始化视图使用
let myView = MyView.instantiate()