Skip to content

Instantly share code, notes, and snippets.

@tooszovski
Created October 2, 2017 14:49
Show Gist options
  • Save tooszovski/afce7f3d36b60fc4cdb77796583ffb86 to your computer and use it in GitHub Desktop.
Save tooszovski/afce7f3d36b60fc4cdb77796583ffb86 to your computer and use it in GitHub Desktop.
IBLoadXib
@IBDesignable
class XibView : UIView {
var contentView:UIView?
@IBInspectable var nibName:String?
override func awakeFromNib() {
super.awakeFromNib()
xibSetup()
}
func xibSetup() {
guard let view = loadViewFromNib() else { return }
view.frame = bounds
view.autoresizingMask =
[.flexibleWidth, .flexibleHeight]
addSubview(view)
contentView = view
}
func loadViewFromNib() -> UIView? {
guard let nibName = nibName else { return nil }
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: nibName, bundle: bundle)
return nib.instantiate(
withOwner: self,
options: nil).first as? UIView
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
xibSetup()
contentView?.prepareForInterfaceBuilder()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment