CustomView from xib

fabiodelorenzo
Posts: 65
Joined: Thu Oct 03, 2013 5:54 pm

CustomView from xib

Postby fabiodelorenzo » Fri Feb 19, 2016 7:45 pm

create a new xib file (File->New->File) and select: User Interface->View
Calle it: CustomViewXib.xib
open the file with IB and add your graphic


create a new CustomView.swift

Code: Select all

import UIKit

class CustomView: UIView {

    class func instanceFromNib() -> UIView {
        return UINib(nibName: "CustomViewXib", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! UIView
    }
   
}


In the CustomViewXib.xib, Identity Inspector, in the Class top field, set CustomView (from the Class in the swift file)

Now, add the custom view programmatically:
in the ViewController.swift add:

Code: Select all

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
       
        let mView = CustomView.instanceFromNib()
        view.addSubview(mView)
    }

Return to “UIView”

Who is online

Users browsing this forum: No registered users and 1 guest