【发布时间】:2018-07-05 08:46:51
【问题描述】:
我尝试使用 Nib 文件实现自定义加载器视图。但我得到一个错误
在loadViewFromNib() 在
return nib.instantiate(withOwner: self, options: nil).first as? UIView.
线程 1:EXC_BAD_ACCESS(代码=2,地址=0x7ffee6f3df98)
open class LoaderView: UIView {
@IBOutlet var loaderImage: UIImageView!
@IBOutlet var contentView: UIView!
func xibSetup() {
contentView = loadViewFromNib()
contentView.frame = bounds
contentView.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
addSubview(contentView)
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xibSetup()
}
override init(frame: CGRect) {
super.init(frame: frame)
xibSetup()
}
func loadViewFromNib() -> UIView? {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "LoaderView", bundle: bundle)
return nib.instantiate(withOwner: self, options: nil).first as? UIView
}
}
我在 VC 中将 viewDidLoad() 中的这个加载器称为
let loader = LoaderView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
view.addSubview(loader)
【问题讨论】:
-
确保您在 XIB 的 FilesOwner 中设置了 class
LoaderView