Custom view will shows up when call below code after awakeFromXib function.

  1. For using frame.
  2. For using constraint.
extention UIView {

    func addViewFromXib() {
        let metaType = type(of: self)
        let bundle = Bundle(for: metaType)
        let nib = UINib(nibName: String(describing: metaType), bundle: bundle)
        if let view = nib.instantiate(withOwner: self, options: nil)[0] as? UIView {
            view.frame = bounds
            view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            addSubview(view)
        }
    }

    func addViewFromXib() {
        let metaType = type(of: self)
        let bundle = Bundle(for: metaType)
        let nib = UINib(nibName: String(describing: metaType), bundle: bundle)
        if let view = nib.instantiate(withOwner: self, options: nil)[0] as? UIView {
            addSubview(view)
            NSLayoutConstraint.activate([
                view.leadingAnchor.constraint(equalTo: leadingAnchor),
                view.trailingAnchor.constraint(equalTo: trailingAnchor),
                view.topAnchor.constraint(equalTo: topAnchor),
                view.bottomAnchor.constraint(equalTo: bottomAnchor)
                ])
        }
    }

}