protocol XibReusable: AnyObject { var contentView: UIView! { get set } func xibSetup() } extension XibReusable where Self: UIView { func xibSetup() { contentView = loadViewFromNib() contentView.frame = bounds contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight] addSubview(contentView) } func loadViewFromNib() -> UIView { let bundle = Bundle(for: type(of: self)) let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle) let view = nib.instantiate(withOwner: self, options: nil).first as? UIView return view! } }