Skip to content

Instantly share code, notes, and snippets.

@VladimirBrejcha
Created August 1, 2020 12:32
Show Gist options
  • Save VladimirBrejcha/4111934209f2a24ead429c1eb5fd903c to your computer and use it in GitHub Desktop.
Save VladimirBrejcha/4111934209f2a24ead429c1eb5fd903c to your computer and use it in GitHub Desktop.
simplify loading views from nib file
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