Aligning collection view cells to fit exactly 3 per row

前端 未结 5 1714
深忆病人
深忆病人 2020-12-23 22:01

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

My collection view data sources are:

func n         


        
相关标签:
5条回答
  • 2020-12-23 22:39

    1- You have to conform to UICollectionViewDelegateFlowLayout

    2- You should implement collectionView(_:layout:sizeForItemAt:) method, as follows:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let screenWidth = UIScreen.main.bounds.width
        let scaleFactor = (screenWidth / 3) - 6
    
        return CGSize(width: scaleFactor, height: scaleFactor)
    }
    

    Assuming that the minimum spaces are 8 -for example-,

    the output should be three items for each row, square sized.

    Remark: If you want to set the spaces between the cells to be zero, then scaleFactor should be:

    let scaleFactor = (screenWidth / 3)
    

    Hope this helped.

    0 讨论(0)
  • 2020-12-23 22:43

    You have to implement the

    UICollectionViewDelegateFlowLayout

    for the spacing stuff.

    Set the size of the collectionViewCells like this:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let yourWidth = collectionView.bounds.width/3.0
        let yourHeight = yourWidth
    
        return CGSize(width: yourWidth, height: yourHeight)
    }
    

    You can also add these functions to get your spacing of the collectionViewCells correct:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets.zero
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    0 讨论(0)
  • 2020-12-23 23:02

    Swift - Version 4.2

    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) 
    }
    
    0 讨论(0)
  • 2020-12-23 23:02

    You have to implement the

    UICollectionViewDelegateFlowLayout for the spacing stuff.

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return  10
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "UpperCollectionViewCell", for: indexPath) as! UpperCollectionViewCell
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
        return CGSize(width: (self.collectionView.frame.width/3.0) - 5 , height: (self.collectionView.frame.height/4.0))
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return  2
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return  2
    }
    
    0 讨论(0)
  • 2020-12-23 23:05

    you can use this

    This code is somehow written that you can change section inset or minimumInteritemSpacing and this calculate and resize with this parameters

    you can use this code or download project from Github

     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
    
            let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
    
            let numberofItem: CGFloat = 3
    
            let collectionViewWidth = self.collectionView.bounds.width
    
            let extraSpace = (numberofItem - 1) * flowLayout.minimumInteritemSpacing
    
            let inset = flowLayout.sectionInset.right + flowLayout.sectionInset.left
    
            let width = Int((collectionViewWidth - extraSpace - inset) / numberofItem)
    
            print(width)
    
            return CGSize(width: width, height: width)
        }
    
    
    0 讨论(0)
提交回复
热议问题