Guest User

Untitled

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