Last active
April 20, 2018 12:07
-
-
Save bielikb/8b48cfeaa56898409132d9c6bdc16b2a to your computer and use it in GitHub Desktop.
Extension to load uiview from nib
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
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage: e.g.
EmptyView.loadFromNib()