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

Swift iOS -Adding ChildViewController to CollectionView Section using SegmentedControl

我有一个视图控制器,其中包含一个包含 2 个部分的 collectionView。第二部分的 header 是一个粘性的 header 并且它里面有一个 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()

The problem is when the segment is selected the collectionView isn't showing any of the color view controllers it's supposed to show. 如何使用 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 内部引用的容器视图控制器。但是您将它们中的每一个都添加到 ContainerVC 最顶层视图中,据我从您的代码中可以看出,其 frame 可能从未设置过。

可能是CGRectZero

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

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