Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ParentViewController
- --collectionView
- --sectionOne
- --sectionTwo
- sectionTwoHeader
- [RedVC, BlueVC, GreenVC] // these should be the size of sectionTwo
- // each of of these color vcs have collectionViews inside of them
- RedCollectionViewController(), BlueCollectionViewController(), GreenCollectionViewController()
- class ParentViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
- var collectionView: UICollectionView!
- let containerController = ContainerController()
- var containerController: ContainerController! // initialized in viewDidLoad
- var vc: UIViewController!
- @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
- }
- /*
- // 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
- */
- }
- }
- 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
- }
- }
Add Comment
Please, Sign In to add comment