Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
// MARK: Support for instantiation from NIB
public extension HasNib where Self: UIView {
static var nib: UINib {
return UINib(nibName: String(describing: self), bundle: Bundle(for: self))
// Adds content loaded from the nib to the end of the
// receiver's list of subviews and adds constraints automatically.
func loadNibContent() {
let layoutAttributes: [NSLayoutAttribute] = [.top, .leading, .bottom, .trailing]
for view in Self.nib.instantiate(withOwner: self, options: nil) {
if let view = view as? UIView {
view.translatesAutoresizingMaskIntoConstraints = false
layoutAttributes.forEach { attribute in
item: view,
attribute: attribute,
relatedBy: .equal,
toItem: self,
attribute: attribute,
multiplier: 1,
constant: 0.0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment