Created
March 29, 2020 04:49
-
-
Save JeremyXue77/a5147fdf8f0ef3db74958cbfa4a2b8dd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| protocol NibOwnerLoadable: AnyObject { | |
| static var nib: UINib { get } | |
| } | |
| // MARK: - Default implmentation | |
| extension NibOwnerLoadable { | |
| static var nib: UINib { | |
| UINib(nibName: String(describing: self), bundle: Bundle(for: self)) | |
| } | |
| } | |
| // MARK: - Supporting methods | |
| extension NibOwnerLoadable where Self: UIView { | |
| func loadNibContent() { | |
| guard let views = Self.nib.instantiate(withOwner: self, options: nil) as? [UIView], | |
| let contentView = views.first else { | |
| fatalError("Fail to load \(self) nib content") | |
| } | |
| self.addSubview(contentView) | |
| contentView.translatesAutoresizingMaskIntoConstraints = false | |
| contentView.topAnchor.constraint(equalTo: topAnchor).isActive = true | |
| contentView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true | |
| contentView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true | |
| contentView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment