简体   繁体   English

在容器视图中的容器视图中显示视图控制器 didSelectItemAtIndexPath swift

[英]Present view controller in a container view at collection view didSelectItemAtIndexPath swift

I am trying to present a view controller in a container view when i select a cell from a collection view.当我从集合视图中选择一个单元格时,我试图在容器视图中显示一个视图控制器。 The problem is that I can't seem to understand how to present it in the container below the collection view.问题是我似乎无法理解如何在集合视图下方的容器中呈现它。 在此处输入图片说明

I tried:我试过:

if (indexPath.row == 0){

                // Presenting first view controller

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

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

How can I make it go to the container instead of presenting the whole view controller.我怎样才能让它进入容器而不是呈现整个视图控制器。 Thanks in advance!提前致谢!

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

Swift 5.0 Use this functions to add and remove child VC: Swift 5.0 使用此函数添加和删除子 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()
    }

adopted from here这里采用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM