Collection View cells to fit exactly 3 per row in all devices swift.

huangapple go评论64阅读模式
英文:

Collection View cells to fit exactly 3 per row in all devices swift

问题

Sure, here's the translated code portion:

我正在尝试创建一个图像的集合视图,以便每行有3个图像,并且它们之间有一些间距。

代码:-

func setupCollectioView(){
    guard let collectionView = gridPhotosCollectionview, let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else { return }
    flowLayout.minimumInteritemSpacing = 2
    flowLayout.minimumLineSpacing = 2
    flowLayout.sectionInset = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
    gridPhotosCollectionview.delegate = self
    gridPhotosCollectionview.dataSource = self
}


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

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

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let noOfCellsInRow = 3
    let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
    let totalSpace = flowLayout.sectionInset.left + flowLayout.sectionInset.right + (flowLayout.minimumInteritemSpacing * CGFloat(noOfCellsInRow - 1))
    let size = Int((collectionView.bounds.width - totalSpace) / CGFloat(noOfCellsInRow))
    return CGSize(width: size, height: size)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    let totalCount = selectedImages.count + selectedPostDetailImages.count
    if totalCount == 1 {
        let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: collectionView.frame.width - flowLayout.itemSize.width)
    }
    return UIEdgeInsets.zero
}

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

Please note that I've translated the code, and if you have any questions or need further assistance with it, feel free to ask.

英文:

I am trying to make a collection view of images, so that there are 3 per row, with some spacing in between.

Code:-

func setupCollectioView(){
    guard let collectionView = gridPhotosCollectionview, let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else { return }
    flowLayout.minimumInteritemSpacing = 2
    flowLayout.minimumLineSpacing = 2
    flowLayout.sectionInset = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
    gridPhotosCollectionview.delegate = self
    gridPhotosCollectionview.dataSource = self
}


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

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

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let noOfCellsInRow = 3
    let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
    let totalSpace = flowLayout.sectionInset.left + flowLayout.sectionInset.right + (flowLayout.minimumInteritemSpacing * CGFloat(noOfCellsInRow - 1))
    let size = Int((collectionView.bounds.width - totalSpace) / CGFloat(noOfCellsInRow))
    return CGSize(width: size, height: size)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    let totalCount = selectedImages.count + selectedPostDetailImages.count
    if totalCount == 1 {
        let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: collectionView.frame.width - flowLayout.itemSize.width)
    }
    return UIEdgeInsets.zero
}

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

The above code works on all the devices from iPhone 11. it's not working below the iPhone 11. Please check the attached screenshots.

iPhone 11 Screen Shot:-

Collection View cells to fit exactly 3 per row in all devices swift.

iPhone XS Screen Shot:-

Collection View cells to fit exactly 3 per row in all devices swift.

Question: How to set the cells frame to fit exactly 3 per row in all devices, Swift?

Can someone please explain to me how to do this, I've tried with the above code but have no results yet. Please Correct me if I'm doing wrong.

Any help would be greatly appreciated.

答案1

得分: 1

这是我的错误,我没有将估计的大小设置为无。现在它正常工作。

英文:

It's my mistake I didn't set the estimated size to none. It's working fine now.

Collection View cells to fit exactly 3 per row in all devices swift.

huangapple
  • 本文由 发表于 2023年5月17日 15:53:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定