对于Swift 4
extension UIView { class func loadFromNibNamed(nibNamed: String, bundle: Bundle? = nil) -> UIView? { return UINib( nibName: nibNamed, bundle: bundle ).instantiate(withOwner: nil, options: nil)[0] as? UIView }}
对于Swift 3
您可以在UIView上创建扩展:
extension UIView { class func loadFromNibNamed(nibNamed: String, bundle: NSBundle? = nil) -> UIView? { return UINib( nibName: nibNamed, bundle: bundle ).instantiateWithOwner(nil, options: nil)[0] as? UIView }}
注意 :使用UINib更快,因为它可以为您缓存。
然后,您可以执行以下 *** 作:
ViewDetailItem.loadFromNibNamed("ViewBtnWishList")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)