I want to embed a UIViewController inside a UIView. I want to create this programmatically. I have created the UIViewController inside the storyboard.
My code to create a empty UIView:
let myNewView=UIView(frame: CGRect(x: (0 + screenHeight / 2), y: leftView.frame.origin.y, width: screenHeight / 2, height: leftView.frame.height))
myNewView.backgroundColor=UIColor.lightGray
self.view.addSubview(myNewView)
And the code to append the UIViewController to the view:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var controller: UIViewController = storyboard.instantiateViewController(withIdentifier: "testView") as UIViewController
myNewView.addSubview(controller.view)
This displays the view inside my UIView, but not at the correct way. The UIView is in this case 512 pixels wide. While the (embeded) UIViewcontroller thinks that is is 1024 pixels wide (the full screen width).
How can I fix it that the embeded view gets the width and height from its parent (the UIView)?
Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy. A view controller's main responsibilities include the following: Updating the contents of the views, usually in response to changes to the underlying data.
Open Main. storyboard and select the view controller of the scene that is already present. Open the Identity Inspector on the right and set Class to MasterViewController in the Custom Class section. With the view controller selected, choose Embed In > Navigation Controller from the Editor menu.
They are separate classes: UIView is a class that represents the screen of the device of everything that is visible to the viewer, while UIViewController is a class that controls an instance of UIView, and handles all of the logic and code behind that view.
As others said you can't embed a viewcontroller view inside a view.
What you can do is embed a ViewController
inside another ViewController
as a ChildViewController
.
Try replacing your newView code with:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var controller: UIViewController = storyboard.instantiateViewController(withIdentifier: "testView") as UIViewController
//add as a childviewcontroller
addChildViewController(controller)
// Add the child's View as a subview
self.view.addSubview(controller.view)
controller.view.frame = view.bounds
controller.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// tell the childviewcontroller it's contained in it's parent
controller.didMove(toParentViewController: self)
EDIT: To change how and where the childviewcontroller appears, simply update its frame. for example to make it half the height and anchored to the bottom:
controller.view.frame = CGRect(x: 0, y: view.center.y, width: view.size.width, height: view.size.height * 0.5)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With