Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change view of my viewController programmatically?

I can change the class of the view in an UIViewController using the Storyboard like so: enter image description here

How can I achieve the same effect programmatically? I've tried this:

convenience init() {
    print("convenience init")
    self.init(nibName: nil, bundle: nil)
    self.view = MTKView()
}

But it doesn't work. Seems to change the view for the MTKView, but the frame doesn't resize like it does when doing this by the Storyboard.

like image 439
Vladislav Avatar asked Jan 05 '23 16:01

Vladislav


1 Answers

You should implement the loadView function in your view controller and assign an instance of your custom view to the view controller's view property:

override func loadView() {
    self.view = MyCustomView(frame: ...)
}
like image 137
Paulw11 Avatar answered Jan 15 '23 19:01

Paulw11