@todo add more info, but when instantiating it from a storyboard/xib:
First, we need to add an extension to UIView:
extension UIView {
public class func loadNib<T: UIView>() -> T {
return Bundle(for: T.self).loadNibNamed(String(describing: T.self), owner: nil, options: nil)![0] as! T
}
}
Now create a UIView subclass and a nib file. Make sure to perform the following steps:
We are now ready to use the class. Simply instantiate it like so:
// In some view controller...
let customView: CustomView = CustomView.loadNib()
self.view.addSubview(customView)
Make sure to explicitly declare the type, otherwise it will try to create a UIView class and fail.