参考链接:swift用xib 自定义View
1> 创建一个view以及同名的xib
2> 在view中添加下边相关代码,完成与xib的链接
// 初始化方法
static func newInstance() -> NetStateView?{
let nibView = Bundle.main.loadNibNamed("NetStateView", owner: nil, options: nil)
if let view = nibView?.first as? NetStateView{
return view
}
return nil
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
load_init()
}
func load_init(){
}
3> 使用初始化方法创建view
lazy var netStateView: NetStateView = {
() -> NetStateView in
let netStateView = NetStateView.newInstance()
if isRootViewController && !IsIphoneX {
netStateView?.frame = CGRect(x: 0, y: 0, width: WIDTH, height: HEIGHT)
} else {
netStateView?.frame = CGRect(x: 0, y: 0, width: WIDTH, height: HEIGHT-Navigation_StatusHeight)
}
return netStateView!
}()