Jimmy lemieux Jimmy lemieux - 1 month ago 21
Swift Question

Loading a XIB file to a UIView Swift

I am trying to load my

XIB
file into a
UIView
but I am having some trouble. I have the required override functions but they seem to be crashing. Saying this error, warning:


could not load any Objective-C class information. This will
significantly reduce the quality of type information available.


I was wondering if someone could show me how to properly load the
XIB
file into a
UIView


import UIKit

class Widget: UIView {

let view = UIView()

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

//call function

loadNib()

}

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

loadNib()

//fatalError("init(coder:) has not been implemented")
}

func loadNib() {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "nib", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
view.frame = bounds
view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
self.addSubview(view);
}
}

Answer

I uses this in one of our projects, maby its usefull to you

import UIKit

class RegisterPageView: UIView {

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