Skip to content

Instantly share code, notes, and snippets.

@bielikb
Last active April 20, 2018 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bielikb/8b48cfeaa56898409132d9c6bdc16b2a to your computer and use it in GitHub Desktop.
Save bielikb/8b48cfeaa56898409132d9c6bdc16b2a to your computer and use it in GitHub Desktop.
Extension to load uiview from nib
extension UIView {
///
/// Loads a view from nib.
///
/// Please note that if you are assigning directly to an optinonal or unwrapped
/// optional you have to specify `name` of the nib.
///
class func loadFromNib<T>(_ name: String = String(describing: T.self), owner: AnyObject? = nil, options: [AnyHashable: Any]? = nil, bundle: Bundle? = Bundle.main) -> T {
return UINib(nibName: name, bundle: bundle).instantiate(withOwner: owner, options: options).first as! T
}
class func loadFromNib<T>(_ name: String = String(describing: T.self), index: Int) -> T {
return UINib(nibName: name, bundle: nil).instantiate(withOwner: nil, options: nil)[index] as! T
}
}
@bielikb
Copy link
Author

bielikb commented Apr 20, 2018

usage: e.g. EmptyView.loadFromNib()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment