Created
October 2, 2017 14:49
-
-
Save tooszovski/afce7f3d36b60fc4cdb77796583ffb86 to your computer and use it in GitHub Desktop.
IBLoadXib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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