在集合视图的容器视图中呈现视图控制器 didSelectItemAtIndexPath swift

Present view controller in a container view at collection view didSelectItemAtIndexPath swift

当我 select 来自集合视图的单元格时,我试图在容器视图中呈现视图控制器。问题是我似乎无法理解如何在集合视图下方的容器中呈现它。

我试过:

if (indexPath.row == 0){

                // Presenting first view controller

    let detailedViewController: ViewController =
          self.storyboard!.instantiateViewControllerWithIdentifier("ViewController") as! ViewController

                self.presentViewController(detailedViewController, animated: true, completion: nil)

如何让它进入容器而不是呈现整个视图控制器。 提前致谢!

self.addChildViewController(detailedViewController)
containerView.addSubview(detailedViewController.view)

Swift 5.0 使用此函数添加和删除child VC:

private func add(asChildViewController viewController: UIViewController, childFrame:CGRect) {
        // Add Child View Controller
        addChild(viewController)

        // Add Child View as Subview
        view.addSubview(viewController.view)

        // Configure Child View
        viewController.view.frame = childFrame
        viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

        // Notify Child View Controller
        viewController.didMove(toParent: self)

    }
    private func remove(asChildViewController viewController: UIViewController) {
        // Notify Child View Controller
        viewController.willMove(toParent: nil)

        // Remove Child View From Superview
        viewController.view.removeFromSuperview()

        // Notify Child View Controller
        viewController.removeFromParent()
    }

采纳自here