我想在我的集合视图中的单元格之间留出不同的间距。
我的收藏夹只包含一个部分。我知道可以改变部分之间的间距,但我喜欢在细胞之间。
我希望中心单元格与其边之间的间距更大,并且其他单元格的间距不会移动。基本上,我的白色圆圈不会触及相邻单元格的边缘而不会改变所有间距。
我的问题图片在这里:
有解决方案吗?
答案 0 :(得分:1)
您可以在视图控制器中实现此方法:collectionView:layout:minimumInteritemSpacingForSectionAtIndex
答案 1 :(得分:1)
您可以尝试创建3个不同的部分。
inter item spacing as 10
section insets as (top: 0, left: 20, bottom: 0, right: 20)
inter item spacing as 10
您可以根据自己的要求更改值。
以下是代码所说的内容:
func numberOfSections(in collectionView: UICollectionView) -> Int
{
return 3
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
if section == 1
{
return 1
}
return 2
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
if indexPath.section == 1
{
cell.backgroundColor = UIColor.red
}
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
if indexPath.section == 1
{
return CGSize(width: 100, height: 100)
}
return CGSize(width: 82, height: 82)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
{
if section == 1
{
return 0
}
return 5
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
if section == 1
{
return UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
}
return .zero
}
<强>截图:强>
如果您仍然遇到任何问题,请告诉我。