Skip to content

Instantly share code, notes, and snippets.

@chanonly123
Last active May 2, 2020 07:13
Show Gist options
  • Save chanonly123/3e8bdb539a9e9d2b148d7952273c7982 to your computer and use it in GitHub Desktop.
Save chanonly123/3e8bdb539a9e9d2b148d7952273c7982 to your computer and use it in GitHub Desktop.
Instantiating custom UIView from xib
import UIKit
extension UIView {
static func initViewFromNib() -> Self {
let nibName = String(describing: self)
let index = 0
let view = Bundle.main.loadNibNamed(nibName, owner: nil, options: nil)![index]
if let castedView = view as? Self {
return castedView
} else {
assertionFailure("Cannot cast to \(nibName)")
return Self()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment