0

the issue: only on iPhone 11, I have two columns of cells, with very big space in the center, on other devices I have 3 cells on 2 columns, I'd like same result on iPhone 11 too

at this moment my solution is minimumInteritemSpacingForSectionAt from 16 to 5, is there a better solution?

enter image description here

what I am using:

let sectionInsets = UIEdgeInsets(top: 16.0, left: 16.0, bottom: 16.0, right: 16.0)


private func configureCollectionView(){
    self.collectionView.collectionViewLayout = UICollectionViewFlowLayout()
    self.collectionView.register(UINib(nibName: "AmountCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell")
    self.collectionView.delegate = self
    self.collectionView.dataSource = self
}


extension SavingsSettingViewController: UICollectionViewDataSource {


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

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.viewModel.amountItems.value.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AmountCollectionViewCell
    cell.title = self.viewModel.amountItems.value[indexPath.row].description
    
    return cell
}
}



extension MyController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 16
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 16
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let itemsPerRow = CGFloat(3.0)
    let paddingSpace = sectionInsets.left * (itemsPerRow + 1)
    let availableWidth = view.frame.width - paddingSpace
    let widthPerItem = availableWidth / itemsPerRow
  
    return CGSize(width: widthPerItem, height: 59)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return self.sectionInsets
}
}
biggreentree
  • 1,633
  • 3
  • 20
  • 35
  • tested, not working, at this moment, better solution is to change minimumInteritemSpacingForSectionAt from 16 to 5, do you think is a correct way? – biggreentree Oct 22 '21 at 08:41
  • try this solution @biggreentree to calculate your padding space this may helps you let paddingSpace = (sectionInsets.left + sectionInsets.right) * (itemsPerRow - 1) + 1 – Muhammad Nawaz Oct 22 '21 at 08:59
  • @SandeepBhandari I'm sorry, but your solution is not working properly, the cells are too close to borders, in a way that even the shadow is cut off. Muhammad Nawaz, your solution is working as expected, please, could you explain it in an answer, I'd like to understand better what's happening and I can mark it as solution. – biggreentree Oct 22 '21 at 10:13
  • Thank you for your help!, do you think this is a proper solution? – biggreentree Oct 22 '21 at 10:22
  • @biggreentree: TBH I couldnt understand the logic behind adding section inset left and right and multiplying it with items per row, section inset is the margin section adds, not really why is that needs to be multiplied with items per row, I can understand `minimumInteritemSpacingForSectionAt` being multiplied to items per row and then to that adding `(sectionInsets.left + sectionInsets.right)` which is what I had mentioned in my comments earlier, will be very interesting to wait for answer and its explanation – Sandeep Bhandari Oct 22 '21 at 10:30

0 Answers0