1.先创建一个集成自view的 UseBikeView_one.swift
2.再创建一个同名xib文件,UseBikeView_one.xib
3.把xib文件按如下操作
4.到UseBikeView_one.swift设置如下代码,即可正常使用xib
var XibView:UIView!
//初始化时将xib中的view添加进来
override init(frame: CGRect) {
super.init(frame: frame)
XibView = loadViewFromNib()
print("大小\(frame)")
XibView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
addSubview(XibView)
}
//初始化时将xib中的view添加进来
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
XibView = loadViewFromNib()
addSubview(XibView)
}
//加载xib
func loadViewFromNib() -> UIView {
let className = type(of: self)
let bundle = Bundle(for: className)
let name = NSStringFromClass(className).components(separatedBy: ".").last
let nib = UINib(nibName: name!, bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
return view
}
网友评论