-->

Controlling the gap between cells in UICollectionV

2019-08-06 02:57发布

问题:

My cells seem to have a gap of about 20 points, I want to control that value, but every answer I found regarding this question does not work.

I tried:

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
}

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

without any useful output, also:

layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0

and these two lines also didn't help.

Please note that I'm not using the storyboard!

回答1:

Logically speaking, the gap value should be based on what's the cell size, editing the minimum spaces leads to how the row should be displayed. To make it more clear, consider (imagine) the following scenario:

it's required to display 2 cells for each row, each cell's width should be the half of the screen width, if the minimum spaces have been set to zeros, output will be: both cells should fill the screen width.

So far so good, but what if the cell's width should be more than the half of the screen width (let's consider that it should be the half of the width + 2 points)? the output will be: each row will contains only 1 cell, beside it a gap with the width of the half of screen - 2 points.

So, the solution is to make sure to set the appropriate size for the cells, by implementing collection​View:​layout:​size​For​Item​At​Index​Path:​ correctly. You might want to check this Q&A to know how you can achieve it.



回答2:

Try This

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionat section: Int) -> CGFloat {
print("collectionView===\(collectionView)")
print("collectionViewLayout===\(collectionViewLayout)")
print("section===\(Int(section))")
return 10.0
}

Line spacing

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionat section: Int) -> CGFloat {
print("collectionView===\(collectionView)")
print("collectionViewLayout===\(collectionViewLayout)")
print("section===\(Int(section))")
return 10.0
}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionat section: Int) -> UIEdgeInsets {
print("collectionView===\(collectionView)")
print("collectionViewLayout===\(collectionViewLayout)")
print("section===\(Int(section))")
// return UIEdgeInsetsMake(0,8,0,8);  // top, left, bottom, right
return UIEdgeInsetsMake(20, 20, 20, 20)
// top, left, bottom, right
}


回答3:

Change thus values In properties of uicollection view.. as seen below..