0

this is the code in the ViewController for CollectionView but it doesn't make Cells to be in the center of CollectionView and here's the result:

enter image description here

extension DeviceAndEquipmentViewController : UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

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

    if collectionView === deviceCollectionView {
        guard selectedDeviceList.count != 0 else {
            selectedDeviceList.insert("nil", at: 0)
            return 1
        }
        return selectedDeviceList.count
    } else if collectionView === equipmentCollectionView {
        guard selectedEquipmentList.count != 0 else {
            selectedEquipmentList.insert("nil", at: 0)
            return 1
        }
        return selectedEquipmentList.count
    } else if collectionView === softwareCollectionView {
        guard selectedSoftwareList.count != 0 else {
            selectedSoftwareList.insert("nil", at: 0)
            return 1
        }
        return selectedSoftwareList.count
    } else {
        return 0
    }

}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

    let inset:CGFloat = 10
    return UIEdgeInsets(top: inset, left: inset, bottom: inset, right: inset)

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

                let numberOfItemsPerRow:CGFloat = 1
                 let spacingBetweenCells:CGFloat = 16

                let totalSpacing = (2 * self.spacing) + ((numberOfItemsPerRow - 1) * spacingBetweenCells)

                let width = (deviceCollectionView.bounds.width - totalSpacing) / numberOfItemsPerRow

    if collectionView == deviceCollectionView {

            let device = self.selectedDeviceList[indexPath.row]

            if device != "nil" {
                    return CGSize(width: width, height: 45)
            } else {
                    return CGSize(width: width + 15, height: 55)
        }
    } else if collectionView == equipmentCollectionView {

            let equipment = self.selectedEquipmentList[indexPath.row]
            if equipment != "nil" {
                return CGSize(width: width, height: 45)
            } else {
                return CGSize(width: width + 15, height: 55)
        }
    } else {

        let software = self.selectedSoftwareList[indexPath.row]

            if software != "nil" {
                return CGSize(width: width, height: 45)
            } else {
                return CGSize(width: width + 15, height: 55)
        }
    }


}
}

and here's the setup for each CollectionView FlowLayout

let layout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: spacing, left: spacing, bottom: spacing, right: spacing)
    layout.minimumLineSpacing = spacing
    layout.minimumInteritemSpacing = spacing
    self.equipmentCollectionView?.collectionViewLayout = layout

I've seen the other topics, but it didn't fix my problem, I use XIB for each collection view cell and didn't do all in Code, I appreciate if you help me figure this out.

Arash Afsharpour
  • 1,282
  • 11
  • 22

1 Answers1

0

I found the answer but Still I don't know what is wrong with it that it work in other ViewController but not this one, the only difference between this ViewController and others is that in this one I used more than 1 ViewController. I linked the answer below, so if anyone has this issue can fix it, I used the Second solution and it worked. thanks to Cœur for sharing this.

Answer

Arash Afsharpour
  • 1,282
  • 11
  • 22