https://www.hackingwithswift.com/example-code/uikit/how-to-use-view-controller-containment

 

 private func add(asChildViewcontroller viewController: UIViewController){

         // Add Child View Controller

        addChild(viewController)

         // Add Child View as Subview

        view.addSubview(viewController.view)

        // Configure Child View

        viewController.view.frame = view.bounds

        viewController.view.autoresizingMask = [.flexibleWidth,.flexibleHeight]

        // Notify Child View Controller

        viewController.didMove(toParent: self)

    }

    

    private func move(asChildViewcontroller viewController: UIViewController){

         // Notify Child View Controller

        viewController.willMove(toParent: self)

         // Remove Child View From Superview

        viewController.view.removeFromSuperview()

         // Notify Child View Controller

        viewController.didMove(toParent: self)

    }