Swift iOS-使用SegmentedControl将ChildViewController添加到CollectionView部分

Swift iOS-使用SegmentedControl将ChildViewController添加到CollectionView部分,ios,swift,uicollectionview,uisegmentedcontrol,childviewcontroller,Ios,Swift,Uicollectionview,Uisegmentedcontrol,Childviewcontroller,我有一个视图控制器,它包含一个带有2个部分的collectionView。第二节的标题是粘性标题,其中有一个segmentedControl: ParentViewController --collectionView --sectionOne // because there is specific data in sectionOne I cannot use a PageViewController --sectionTwo

我有一个视图控制器,它包含一个带有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,它将显示对应于每个段的视图控制器:

// each of of these color vcs have collectionViews inside of them
RedCollectionViewController(), BlueCollectionViewController(), GreenCollectionViewController()
问题是,当选定段时,collectionView没有显示它应该显示的任何颜色视图控制器如何使用addChildViewController()将每种颜色的vc添加到collectionView?

带有分段控件的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
        */
    }
}
ContainerVC:

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
    }
}

您正在将红色/蓝色/绿色VCs添加到从ParentViewController内部引用的容器视图控制器。但您正在ContainerVC最上面的视图中添加它们中的每一个,就我从您的代码中看到的情况来看,其
框架可能从未设置过

它可能是
CGRectZero

将子VC视图添加到此视图将导致它们被错误定位,或者根本没有定位。因为容器视图控制器不在视图控制器层次结构中。实际上,您正在ParentViewController的viewDidLoad()中执行所有操作。最有可能的是,ContainerVC的
viewDidLoad
甚至没有被调用。因此,它的观点从未被正确地初始化过


您可能根本不需要ContainerVC。尝试将子项添加到ParentViewController,并尝试在调用
viewDidLoad()
后添加子项,即在
ViewDidDisplay()
viewDidLayoutSubviews()
和切换段选择时添加子项。

为什么选择collectionVC?为什么不使用UIPageViewController?嘿,谢谢你的帮助。我想,因为我有分段控件,每个分段都显示了一个不同的集合视图,分段控件是最好使用的。使用它有什么问题吗?您有
let containerController=containerController()
var containerController:containerController在随后的语句中!而且,
var containerController
未在输入错误的
ViewDidLoad
中初始化。我会修好的。我没有添加viewDidLoad,因为将代码放在那里并没有什么区别,因为不管它在那里初始化,这都是100%编程的。请阅读我上面的评论。由于第一节的原因,我实际上必须使用集合视图