swift3.0通过xib创建view时,一直crash仔细查找了一下找到一个坑爹得点现在总结出来避免再走弯路
一、创建view类和对应的xib文件
二、关联xib文件(关键点,否则会crash)
一定是关联file's Owner上的custom class上。如果关联到view上就会报错
三、加载对应的xib界面(不废话直接上代码)
class test: UIView { override init(frame: CGRect) { super.init(frame: frame) print("init") awakeFromNib() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func awakeFromNib() { super.awakeFromNib() layoutSubviews() } override func layoutSubviews() { super.layoutSubviews() print("layoutSubviews") let view : UIView = Bundle.main.loadNibNamed("test",owner: self,options: nil)?.first as! UIView addSubview(view) } }原文链接:https://www.f2er.com/swift/322632.html