Skip to content

Instantly share code, notes, and snippets.

@DenTelezhkin
Created March 27, 2019 10:35
Show Gist options
  • Save DenTelezhkin/d37c724a5231acab6e4f6edcd86ad27d to your computer and use it in GitHub Desktop.
Save DenTelezhkin/d37c724a5231acab6e4f6edcd86ad27d to your computer and use it in GitHub Desktop.
LoadableFromXibView example for MLSDev blog article.
protocol NibDefinable {
var nibName: String { get }
}
extension NibDefinable {
var nibName : String {
return String(self.dynamicType)
}
}
class LoadableFromXibView: UIView, NibDefinable {
@IBOutlet weak var view : UIView!
override init(frame: CGRect) {
super.init(frame: frame)
xibSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xibSetup()
}
func xibSetup() {
view = loadViewFromXib()
view.frame = bounds
view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
backgroundColor = .clearColor()
addSubview(view)
}
private func loadViewFromXib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: nibName, bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil).first as! UIView
return view
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment