import UIKit extension UIView { static func initViewFromNib() -> Self { let nibName = String(describing: self) let index = 0 let view = Bundle.main.loadNibNamed(nibName, owner: nil, options: nil)![index] if let castedView = view as? Self { return castedView } else { assertionFailure("Cannot cast to \(nibName)") return Self() } } }