Guest User

Untitled

a guest
Jun 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. ParentViewController
  2. --collectionView
  3. --sectionOne
  4. --sectionTwo
  5. sectionTwoHeader
  6. [RedVC, BlueVC, GreenVC] // these should be the size of sectionTwo
  7.  
  8. // each of of these color vcs have collectionViews inside of them
  9. RedCollectionViewController(), BlueCollectionViewController(), GreenCollectionViewController()
  10.  
  11. class ParentViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
  12.  
  13. var collectionView: UICollectionView!
  14. let containerController = ContainerController()
  15. var containerController: ContainerController! // initialized in viewDidLoad
  16. var vc: UIViewController!
  17.  
  18. @objc func selectedIndex(_ sender: UISegmentedControl){
  19.  
  20. let index = sender.selectedSegmentIndex
  21.  
  22. switch index {
  23. case 0:
  24. containerController.vcIdentifierReceivedFromParent(segment: "BlueVC")
  25. break
  26. case 1:
  27. containerController.vcIdentifierReceivedFromParent(segment: "RedVC")
  28. break
  29. case 2:
  30. containerController.vcIdentifierReceivedFromParent(segment: "GreenVC")
  31. break
  32. default: break
  33. }
  34.  
  35. /*
  36. // this adds the containerVC over the collectionView instead of under the sectionTwo segmented Control header
  37. vc = containerController
  38. addChildViewController(vc)
  39. vc.view.frame = CGRect(x: 0,y: 0, width: collectionView.frame.width,height: collectionView.frame.height)
  40. view.addSubview(vc.view)
  41. vc.didMove(toParentViewController: self)
  42. lastViewController = vc
  43. */
  44. }
  45. }
  46.  
  47. class ContainerController: UIViewController {
  48.  
  49. var vc: UIViewController!
  50. var lastViewController: UIViewController!
  51.  
  52. override func viewDidLoad() {
  53. super.viewDidLoad()
  54.  
  55. view.backgroundColor = .white
  56. vcIdentifierReceivedFromParent(segment: "RedVC")
  57. }
  58.  
  59. func vcIdentifierReceivedFromParent(segment: String){
  60.  
  61. switch segment {
  62.  
  63. case "RedVC":
  64.  
  65. let redVC = RedCollectionViewController()
  66. addVcToContainer(destination: redVC)
  67. break
  68.  
  69. case "BlueVC":
  70.  
  71. let blueVC = BlueCollectionViewController()
  72. addVcToContainer(destination: blueVC)
  73. break
  74.  
  75. case "GreenVC":
  76.  
  77. let greenVC = GreenCollectionViewController()
  78. addVcToContainer(destination: greenVC)
  79. break
  80.  
  81. default: break
  82. }
  83. }
  84.  
  85. func addVcToContainer(destination: UIViewController) {
  86.  
  87. //Avoids creation of a stack of view controllers
  88. if lastViewController != nil{
  89. lastViewController.view.removeFromSuperview()
  90. }
  91.  
  92. self.vc = destination
  93. addChildViewController(vc)
  94. vc.view.frame = CGRect(x: 0,y: 0, width: view.frame.width,height: view.frame.height)
  95. view.addSubview(vc.view)
  96. vc.didMove(toParentViewController: self)
  97. lastViewController = vc
  98. }
  99. }
Add Comment
Please, Sign In to add comment