分段控件切换集合视图

Segmented control to switch collection views

无法加载 viewController 秒。 分段控制器没有初始选择

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

这是我的 parent 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()
}

}

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

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

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

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

Tip : Also you better create an extension instead of repeating the 4 lines for each vc