Skip to content

Instantly share code, notes, and snippets.

@chanonly123
Last active May 27, 2020 09:00
Show Gist options
  • Save chanonly123/c650e2f3c770643ded0ce0627eba0fff to your computer and use it in GitHub Desktop.
Save chanonly123/c650e2f3c770643ded0ce0627eba0fff to your computer and use it in GitHub Desktop.
Designable view from xib, without optimisation
@IBDesignable
class UIViewXib: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
loadViewFromNib()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadViewFromNib()
}
private func loadViewFromNib() {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
if let view = nib.instantiate(withOwner: self, options: nil).first as? UIView {
addSubview(view)
view.frame = bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}
// low priority default height constraint
let heightConst = heightAnchor.constraint(equalToConstant: 100)
heightConst.priority = .init(1)
heightConst.isActive = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment