Created
July 16, 2018 21:38
-
-
Save leojkwan/b125a7049baecd7e307cc8f3b1d00f33 to your computer and use it in GitHub Desktop.
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
// 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 | |
self.addSubview(view) | |
layoutAttributes.forEach { attribute in | |
self.addConstraint( | |
NSLayoutConstraint( | |
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