我有一个 View Controller ,其中包含一个包含 2 个部分的 collectionView。第二部分的标题是一个粘性标题,里面有一个segmentedControl:
ParentViewController
--collectionView
--sectionOne // because there is specific data in sectionOne I cannot use a PageViewController
--sectionTwo
sectionTwoHeader // sticky header
[RedVC, BlueVC, GreenVC] // these should be the size of sectionTwo
当选择一个段时,我使用 ContainerVC 来显示与每个段相对应的 View Controller :
// each of of these color vcs have collectionViews inside of them
RedCollectionViewController(), BlueCollectionViewController(), GreenCollectionViewController()
问题是当选择该段时,collectionView没有显示任何它应该显示的颜色 View Controller 。 如何使用 addChildViewController() 将每种颜色 vc 添加到 collectionView 中?
带有segmentedControl的selectedIndex的collectionView:
class ParentViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
var collectionView: UICollectionView!
var containerController: ContainerController!
var vc: UIViewController!
override func viewDidLoad() {
super.viewDidLoad()
containerController = ContainerController()
}
@objc func selectedIndex(_ sender: UISegmentedControl){
let index = sender.selectedSegmentIndex
switch index {
case 0:
containerController.vcIdentifierReceivedFromParent(segment: "BlueVC")
break
case 1:
containerController.vcIdentifierReceivedFromParent(segment: "RedVC")
break
case 2:
containerController.vcIdentifierReceivedFromParent(segment: "GreenVC")
break
default: break
}
/*
// because of the X and Y values this adds the containerVC over the collectionView instead of under the sectionTwo segmented Control header
vc = containerController
addChildViewController(vc)
vc.view.frame = CGRect(x: 0,y: 0, width: collectionView.frame.width,height: collectionView.frame.height)
view.addSubview(vc.view)
vc.didMove(toParentViewController: self)
lastViewController = vc
*/
}
}
容器VC:
class ContainerController: UIViewController {
var vc: UIViewController!
var lastViewController: UIViewController!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
vcIdentifierReceivedFromParent(segment: "RedVC")
}
func vcIdentifierReceivedFromParent(segment: String){
switch segment {
case "RedVC":
let redVC = RedCollectionViewController()
addVcToContainer(destination: redVC)
break
case "BlueVC":
let blueVC = BlueCollectionViewController()
addVcToContainer(destination: blueVC)
break
case "GreenVC":
let greenVC = GreenCollectionViewController()
addVcToContainer(destination: greenVC)
break
default: break
}
}
func addVcToContainer(destination: UIViewController) {
//Avoids creation of a stack of view controllers
if lastViewController != nil{
lastViewController.view.removeFromSuperview()
}
self.vc = destination
addChildViewController(vc)
vc.view.frame = CGRect(x: 0,y: 0, width: view.frame.width,height: view.frame.height)
view.addSubview(vc.view)
vc.didMove(toParentViewController: self)
lastViewController = vc
}
}
最佳答案
您正在将红色/蓝色/绿色 VC 添加到从 ParentViewController 内部引用的容器 View Controller 。但是您将它们中的每一个添加到 ContainerVC 最顶层 View 中,据我从您的代码中看到,其 frame
可能从未设置。
可能是CGRectZero
。
向该 View 添加子 VC View 将导致它们定位错误,或者根本没有定位。因为容器 View Controller 不在 View Controller 层次结构中。您实际上是在 ParentViewController 的 viewDidLoad() 中完成所有操作。最有可能的是,ContainerVC 的 viewDidLoad
甚至没有被调用。因此它的 View 永远不会被正确初始化。
您可能根本不需要 ContainerVC。尝试将子项添加到 ParentViewController,并尝试在 viewDidLoad()
调用之后添加它们,即在 viewDidAppear()
、viewDidLayoutSubviews()
中以及 switch 段上选择。
关于ios - Swift iOS - 使用 SegmentedControl 将 ChildViewController 添加到 CollectionView 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50920790/