Skip to content

Instantly share code, notes, and snippets.

@richimf
Created July 29, 2020 22:11
Show Gist options
  • Save richimf/db89c32c39dc26aafbaeb3ffda6e84e4 to your computer and use it in GitHub Desktop.
Save richimf/db89c32c39dc26aafbaeb3ffda6e84e4 to your computer and use it in GitHub Desktop.
CustomView
import UIKit
class CustomView: UIView {
//Override the init() constructor so that you can call the function loadView()
override init(frame: CGRect) {
super.init(frame: frame)
loadView()
}
// Override the requiered constructor
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func loadView() {
// Bundle contains subfolders so that you can get a reference to the xib file
let bundle = Bundle(for: CustomView.self)
// Create an instance of the .xib indicating its bundle
let nib = UINib(nibName: "CustomView", bundle: bundle)
// Instantiate the view and and assign the owner, with is the current class
if let view = nib.instantiate(withOwner: self).first as? UIView {
view.frame = bounds
// Add the instantiated view to the current view
addSubview(view)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment