Adding a UIViewController inside a UICollectionView Cell

前端 未结 4 2068
既然无缘
既然无缘 2021-01-04 04:22

How would I go about inserting a UIViewController inside a UICollectionView Cell?

4条回答
  •  爱一瞬间的悲伤
    2021-01-04 05:11

    You will need to make a custom container view controller: https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html

    func display(contentController content: UIViewController, on view: UIView) {
        self.addChildViewController(content)
        content.view.frame = view.bounds
        view.addSubview(content.view)
        content.didMove(toParentViewController: self)
    }
    

    For each cell in your container, you will have to call something like the above function with the correct child view controller on the cell's content view.

    Be careful not to try to add multiple view controllers onto the same cell and make sure they get removed properly as well.

    This idea is complex enough that it isn't conducive to a simple stack overflow answer but hopefully the above will be enough to get you started.

提交回复
热议问题