【问题标题】:Swift iOS -Adding ChildViewController to CollectionView Section using SegmentedControlSwift iOS - 使用 SegmentedControl 将 ChildViewController 添加到 CollectionView 部分
【发布时间】: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


【解决方案1】:

您正在向从 ParentViewController 内部引用的容器视图控制器添加红色/蓝色/绿色 VC。但是您将它们中的每一个都添加到 ContainerVC 最顶层视图中,据我从您的代码中可以看出,其 frame 可能永远不会设置。

可能是CGRectZero

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 2015-08-12
    相关资源
    最近更新 更多