Skip to content

Instantly share code, notes, and snippets.

@alvaroroyo
Created March 30, 2020 14:41
Show Gist options
  • Save alvaroroyo/0acc0d6f525f3662eacec47429614f0f to your computer and use it in GitHub Desktop.
Save alvaroroyo/0acc0d6f525f3662eacec47429614f0f to your computer and use it in GitHub Desktop.
class UIXibView: UIView {
public var nibName:String? { return String(describing: type(of: self)) }
public var contentView: UIView!
override public init(frame: CGRect) {
super.init(frame: frame)
awakeFromNib()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override open func awakeFromNib() {
super.awakeFromNib()
guard let nibName = self.nibName,
let view = Bundle.main.loadNibNamed(nibName, owner: self, options: nil)?.first as? UIView
else { return }
self.layoutIfNeeded()
contentView = view
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleHeight,.flexibleWidth]
self.addSubview(contentView)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment