【问题标题】:Segmented control to switch collection views用于切换集合视图的分段控制
【发布时间】:2021-07-26 15:58:17
【问题描述】:

无法加载视图控制器。 分段控制器中没有初始选择

当我的视图加载时,我希望我的 firstCVC 默认是第一个 viewController

这是我的父视图控制器

class covidVC: UIViewController {

private let segmentedController: UISegmentedControl = {
    let labelArr = ["Covid19","Symptoms","WHO","Vaccine"]
    let seg = UISegmentedControl(items: labelArr)
    seg.translatesAutoresizingMaskIntoConstraints = false
    seg.tintColor = .gray
    seg.addTarget(self, action:#selector(segAction(_:)), for: .valueChanged)
    return seg
}()

func viewLoader(){
    
    addChild(FirstCVC())
    addChild(SecondCVC())
    addChild(ThirdCVC())
    addChild(FourthCVC())
    
    self.view.addSubview(FirstCVC().view)
    self.view.addSubview(SecondCVC().view)
    self.view.addSubview(ThirdCVC().view)
    self.view.addSubview(FourthCVC().view)
    
    FirstCVC().didMove(toParent: self)
    SecondCVC().didMove(toParent: self)
    ThirdCVC().didMove(toParent: self)
    FourthCVC().didMove(toParent: self)
    
    FirstCVC().view.frame = self.view.bounds
    SecondCVC().view.frame = self.view.bounds
    ThirdCVC().view.frame = self.view.bounds
    FourthCVC().view.frame = self.view.bounds
}

@objc func segAction(_ segmentedControll: UISegmentedControl){
    
    FirstCVC().view.isHidden = true
    SecondCVC().view.isHidden = true
    ThirdCVC().view.isHidden = true
    FourthCVC().view.isHidden = true

    switch segmentedControll.selectedSegmentIndex {
    case 0:
        FirstCVC().view.isHidden = false
    case 1:
        SecondCVC().view.isHidden = false
    case 2:
        ThirdCVC().view.isHidden = false
    case 3:
        FourthCVC().view.isHidden = false
    default:
        FirstCVC().view.isHidden = false
    }
}

override func viewDidLayoutSubviews() {

    view.addSubview(segmentedController)
    segmentedController.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
    segmentedController.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = true
}

override func viewDidLoad() {
    super.viewDidLoad()
    viewLoader()
}

}

所有四个子视图都是集合VC。我希望他们成为独立的 VC 我不想重新加载集合视图,因为它们有不同的行和列

class FirstCVC: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate{

//    primary horizantal scrollVIew
private let myCollectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical
    layout.itemSize = CGSize(width: 365 , height: 300)
    layout.sectionInset = UIEdgeInsets(top: 10, left: 5, bottom: 10, right: 5)
    
    let view = UICollectionView(frame: .zero, collectionViewLayout:layout)
    view.showsVerticalScrollIndicator = false
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = .red
    return view
    
}()

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 3
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FirstCustomCell", for: indexPath) as! FirstCustomCell
    cell.backgroundColor = .red
    return cell
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    
    view.addSubview(myCollectionView)
    myCollectionView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
    myCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    myCollectionView.widthAnchor.constraint(equalTo: view.safeAreaLayoutGuide.widthAnchor).isActive = true
}

override func viewDidLoad() {
    super.viewDidLoad()
    
    myCollectionView.dataSource = self
    myCollectionView.delegate = self
    myCollectionView.register(FirstCustomCell.self, forCellWithReuseIdentifier: FirstCustomCell.identifier)
}

}

The Image output I'm getting

【问题讨论】:

    标签: ios swift xcode uicollectionview uisegmentedcontrol


    【解决方案1】:

    你每行创建一个不同的实例

    addChild(FirstCVC())
    addChild(SecondCVC())
    addChild(ThirdCVC())
    addChild(FourthCVC())
    
    self.view.addSubview(FirstCVC().view)
    self.view.addSubview(SecondCVC().view)
    self.view.addSubview(ThirdCVC().view)
    self.view.addSubview(FourthCVC().view)
    
    FirstCVC().didMove(toParent: self)
    SecondCVC().didMove(toParent: self)
    ThirdCVC().didMove(toParent: self)
    FourthCVC().didMove(toParent: self)
    
    FirstCVC().view.frame = self.view.bounds
    SecondCVC().view.frame = self.view.bounds
    ThirdCVC().view.frame = self.view.bounds
    FourthCVC().view.frame = self.view.bounds
    

    应该是的

    let vc1 = FirstCVC()
    
    let vc2 = SecondCVC()
    
    let vc3 = ThirdCVC()
    
    let vc4 = FourthCVC()
    
    addChild(vc1)
    addChild(vc2)
    addChild(vc3)
    addChild(vc4)
    
    self.view.addSubview(vc1.view)
    self.view.addSubview(vc2.view)
    self.view.addSubview(vc3.view)
    self.view.addSubview(vc4.view)
    
     vc1.didMove(toParent: self)
     vc2.didMove(toParent: self)
     vc3.didMove(toParent: self)
     vc4.didMove(toParent: self)
    
    vc1.view.frame = self.view.bounds
    vc2.view.frame = self.view.bounds
    vc3.view.frame = self.view.bounds
    vc4.view.frame = self.view.bounds
    

    提示:另外,您最好创建一个扩展,而不是为每个 vc 重复 4 行

    【讨论】:

    • 它解决了我的问题。谢谢你。一旦等待剩余时间结束,我将接受答案。另外,分段控件默认不会突出显示到第一个索引,还有什么需要做的吗
    • 添加segmentedController.selectedSegmentIndex = 0
    • 另一个快速的问题是框架与分段控件重叠。有简单的解决方法吗?我熟悉锚点,但不熟悉框架
    • viewDidLayoutSubviews 中的所有代码都应该在添加所有 vc 的视图之后
    • 任何时候你有 Object() 的构造,你都在创建该对象的全新实例。 (括号调用默认初始化程序。)每个新实例与该对象的先前实例无关。在你的代码中。在每一步创建您的FirstCVCSecondCVC 等的新副本。这几乎总是错误的。
    猜你喜欢
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多