Last active
May 27, 2020 09:00
-
-
Save chanonly123/c650e2f3c770643ded0ce0627eba0fff to your computer and use it in GitHub Desktop.
Designable view from xib, without optimisation
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 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