Created
July 29, 2020 22:11
-
-
Save richimf/db89c32c39dc26aafbaeb3ffda6e84e4 to your computer and use it in GitHub Desktop.
CustomView
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
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