问题描述
我试图在UICollectionView单元内添加UIViewController,但不确定如何这样做.
I am trying to add a UIViewController inside a UICollectionView Cell and wasn't sure how to do so.
推荐答案
您将需要创建自定义容器视图控制器: https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html
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.
这篇关于在UICollectionView单元格内添加UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!