Create a class CustomView.swift and xib named CustomView.xib.
- Open CustomView.xib
- Click on file owner and provide the custom class name.
- Open CustomView.xib
- Click root view and join the view outlet with file owner.
- Open CustomView.swift
- Add following code to load view from xib. Here xib name is same as class name.
func loadViewFromNib() { 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 addSubview(view) view.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ self.topAnchor.constraint(equalTo: view.topAnchor), self.leadingAnchor.constraint(equalTo: view.leadingAnchor), self.trailingAnchor.constraint(equalTo: view.trailingAnchor), self.bottomAnchor.constraint(equalTo: view.bottomAnchor), ]) self.view = view }- Call the loadViewFromNib() from following init methods.
override init(frame: CGRect)required public init?(coder aDecoder: NSCoder)- Open your storyboard or viewcontroller xib.
- Drag UIView and add appropriate constraints.
- Change class for this view to CustomView.
- Create outlet for to use in code.


