Junior — Middle
Is it mandatory to call the parent method via super in the loadView method when overriding?
sobes.tech AI
Answer from AI
In the loadView method of the UIViewController class, calling the parent method via super.loadView() is not mandatory and is most often not done. This is because loadView is intended for manually creating and setting the controller's root view. If you override this method, you should create and assign your own view property.
If you call super.loadView(), the default implementation will be loaded, which attempts to load the view from a storyboard or nib file. Manually creating the view can lead to conflicts or duplication.
Example of overriding loadView without calling super:
override func loadView() {
let customView = UIView()
customView.backgroundColor = .white
self.view = customView
}