【发布时间】:2018-11-27 23:13:57
【问题描述】:
我有一个包含 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
When a segment is selected I'm using a ContainerVC that will show a view controller corresponding to each segment:
// each of of these color vcs have collectionViews inside of them
RedCollectionViewController(), BlueCollectionViewController(), GreenCollectionViewController()
问题是选择段时,CollectionView不会显示它应该显示的任何颜色视图控制器。 如何使用 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
}
}
【问题讨论】:
-
为什么选择collectionVC?为什么不是 UIPageViewController?
-
嘿,感谢您的帮助。我想,因为我有分段控件,并且每个段都显示了分段控件最好使用的不同集合视图。使用有什么问题吗?
-
您在后续语句中有
let containerController = ContainerController()和var containerController: ContainerController!!并且var containerController没有在ViewDidLoad中初始化 -
那是一个错字。我会修复它。我没有添加 viewDidLoad,因为将代码放在那里确实没有什么区别,因为不管它在那里初始化
-
谢谢,这个项目 100% 程序化。阅读我上面的评论。由于第一部分,我实际上必须使用集合视图
标签: ios swift uicollectionview uisegmentedcontrol childviewcontroller