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

时间:2018-06-19 04:46:02

标签: ios swift uicollectionview uisegmentedcontrol childviewcontroller

我有一个视图控制器,它包含一个包含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?

collectionView w / segmentedControl的selectedIndex:

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

1 个答案:

答案 0 :(得分:0)

您正在将红色/蓝色/绿色VC添加到从ParentViewController内部引用的Container View控制器中。但是你要在ContainerVC最顶层的视图中添加它们,其中frame可能永远不会被设置,就我从你的代码中看到的那样。

可能是CGRectZero

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

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