Created
August 1, 2020 12:32
-
-
Save VladimirBrejcha/4111934209f2a24ead429c1eb5fd903c to your computer and use it in GitHub Desktop.
simplify loading views from nib file
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
import UIKit | |
protocol NibLoadable where Self: UIView { | |
static var nibName: String { get } | |
} | |
extension NibLoadable { | |
static var nibName: String { String(describing: Self.self) } | |
static var nib: UINib { UINib(nibName: Self.nibName, bundle: Bundle(for: Self.self)) } | |
func setupFromNib() { | |
guard let view = Self.nib.instantiate(withOwner: self, options: nil).first as? UIView | |
else { fatalError("Error loading \(self) from nib") } | |
addSubview(view) | |
view.frame = bounds | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment