Creating custom user interface files with XIB in Xcode 6 and Swift

In this video I am showing you how to create custom user interface file with XIB in Xcode 6 and how to load it as a Subview programatically as well as using Main.storyboard.

Source code of MyCustomView file:

import UIKit

@IBDesignable
class MyCustomView: UIView {

@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var myImage: UIImageView!
@IBOutlet weak var myButton: UIButton!

var view:UIView!

@IBInspectable
var mytitleLabelText: String? {
get {
return titleLabel.text
}
set(mytitleLabelText) {
titleLabel.text = mytitleLabelText
}
}

@IBInspectable
var myCustomImage:UIImage? {
get {
return myImage.image
}
set(myCustomImage) {
myImage.image = myCustomImage
}
}

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

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

func setup() {
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeightaddSubview(view)
}

func loadViewFromNib() -> UIView {
let bundle = NSBundle(forClass:self.dynamicType)
let nib = UINib(nibName: “MyCustomView”, bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as UIView

return view
}
}