Search code examples
swiftuiviewcontrollerviewcontrollerembedding

Is embedding a UIViewController better than adding a subview to a UIViewController (programmatically)


What's the difference between adding a viewcontroller's view inside another view controller's view and embedding a view controller like this:

 addChildViewController(controller)

 self.view.addSubview(controller.view)
 controller.view.frame = view.bounds
 controller.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

 controller.didMove(toParentViewController: self)

Solution

  • Embedding a view controller (View Controller Containment) has added benefits such as calls being made to viewDidAppear, viewDidDisappear and rotation and resizing events being passed to the view controller to handle. These don't happen when you just add a view directly.