Xib UIView Load On Storyboard

ViewController.swift

import UIKit

class ViewController: UIViewController {

    var xibView: UIView?

    override func viewDidLoad() {

        super.viewDidLoad()

        print(“usa”)

        xibView = ViewTemp.instanceFromNibForViewTemp()

        xibView?.frame = CGRect(x: 0, y: 0, width: 300, height: 500)

        xibView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]

        self.view.addSubview(xibView!)

        // Do any additional setup after loading the view, typically from a nib.

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    @IBAction func buttonActionRemoveView(_ sender: AnyObject) {

        self.xibView?.removeFromSuperview()

    }

}

ViewTemp.swift

import UIKit

class ViewTemp: UIView {

    /*

     // Only override draw() if you perform custom drawing.

     // An empty implementation adversely affects performance during animation.

     override func draw(_ rect: CGRect) {

     // Drawing code

     }

     */

    class func instanceFromNibForViewTemp() -> UIView {

        return UINib(nibName: “ViewTemp”, bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView

    }

    override init(frame: CGRect) {

        super.init(frame: frame)

        nibSetup()

    }

    required init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)

        nibSetup()

    }

    private func nibSetup() {

        let myView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

        myView.backgroundColor = UIColor.blue

        self.addSubview(myView)

    }

    @IBAction func buttonActionClickHere(_ sender: AnyObject) {

        print(“usa”)

    }

}

NibLoadingView.swift

import UIKit

// Usage: Subclass your UIView from NibLoadView to automatically load a xib with the same name as your class

protocol NibDefinable {

    var nibName: String { get }

}

@IBDesignable

class NibLoadingView: UIView, NibDefinable {

    @IBOutlet weak var view: UIView!

    var nibName: String {

        return String(describing: type(of: self))

    }

    override init(frame: CGRect) {

        super.init(frame: frame)

        nibSetup()

    }

    required init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)

        nibSetup()

    }

    private func nibSetup() {

        backgroundColor = UIColor.clear

        view = loadViewFromNib()

        view.frame = bounds

        view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

        view.translatesAutoresizingMaskIntoConstraints = true

        addSubview(view)

    }

    private func loadViewFromNib() -> UIView {

        let bundle = Bundle(for: type(of: self))

        let nib = UINib(nibName: nibName, bundle: bundle)

        let nibView = nib.instantiate(withOwner: self, options: nil).first as! UIView

        return nibView

    }

}

Download Sample Project From Github

Leave a comment