提问者:小点点

如何基于屏幕限制集合视图间距


我正在通过函数(在此问题中检索到)根据设备的屏幕大小来设置CollectionView的单元格的大小。 但是,我有一个问题的单元格间距。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let width = UIScreen.main.bounds.width
    return CGSize(width: (width - 10)/3, height: (width - 10)/3) // width & height are the same to make a square cell
}

iPhone SE 2在iPhone SE 2上显示良好的收集视图

iPhone XS Max太大间距收集视图


共1个答案

匿名用户

问题在于最小行空间,因为您在定义单元格宽度时为单元格的行分配空间,所以将最小行空间设置为零是安全的。

请使用下列方法之一

>

  • 在代码
    扩展名YourViewController中:UICollectionViewDataSourcePrefetching{

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

    1. 在情节提要中