anisha
anisha

Reputation: 61

how to create a custom view using xib

I have created a custom view which can be reuse at number of places,I have assigned this classname to the sub view of view controller.Here I am completely using XIB's.while running this app it's getting crashed and throughing an error "Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeee23be2c)" at the line of " Bundle.main.loadNibNamed("TripCancelAlert", owner: self, options: nil)"

View class:

class TripCancelAlert: UIView {


    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    func commonInit() {
        Bundle.main.loadNibNamed("TripCancelAlert", owner: self, options: nil)
       // self.addSubview(contentView)
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

Upvotes: 0

Views: 54

Answers (1)

CZ54
CZ54

Reputation: 5588

Find below the superclass for your view.

Be sure to name your xib with the same name as your class ( MyView.xib / MyView )

class NibView: UIView {

    @IBOutlet var view: UIView!

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    func commonInit() {
        guard let viewFromNib = Bundle.main.loadNibNamed(className, owner: self, options: nil)?.first as? UIView
            else { fatalError("Could not load view from nib file.") }
        view = viewFromNib
        translatesAutoresizingMaskIntoConstraints = false
        view.translatesAutoresizingMaskIntoConstraints = false
        addPinnedSubview(view)

    }

    func addPinnedSubview(_ view: UIView, withInsets insets: UIEdgeInsets = .zero) {
        addSubview(view)
        let viewsDict: [String: UIView] = ["childView": view]
        var metrics: [String: Any] = [:]
        metrics["topSpace"] = insets.top
        metrics["bottomSpace"] = insets.bottom
        metrics["leftSpace"] = insets.left
        metrics["rightSpace"] = insets.right
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(topSpace)-[childView]-(bottomSpace)-|",
                                                      options: [],
                                                      metrics: metrics,
                                                      views: viewsDict))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(leftSpace)-[childView]-(rightSpace)-|",
                                                      options: [],
                                                      metrics: metrics,
                                                      views: viewsDict))
    }
}

Upvotes: 1

Related Questions