对齐集合视图单元格以精确匹配每行3个

Kin*_*Tim 11 xcode ios uicollectionview swift

我正在尝试制作图像的集合视图,因此每行有3个,其间没有间距.

我的集合视图数据源是:

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

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return posts.count
}
Run Code Online (Sandbox Code Playgroud)

这就是它在我的故事板中设置的方式(imageView宽度为125,是375屏幕宽度的三分之一):

在此输入图像描述

但是,当我运行应用程序并添加一些照片时,它看起来像这样:

在此输入图像描述

我如何解决这个问题,以便每行看到3张图片?谢谢你的帮助!

Kin*_*one 38

你必须实现

UICollectionViewDelegateFlowLayout

对于间距的东西.

像这样设置collectionViewCells的大小:

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)
}
Run Code Online (Sandbox Code Playgroud)

您还可以添加这些函数以使collectionViewCells的间距正确:

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
}
Run Code Online (Sandbox Code Playgroud)


Gag*_*hir 13

Swift-版本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) 
}
Run Code Online (Sandbox Code Playgroud)


Ahm*_*d F 8

1-你必须符合UICollectionViewDelegateFlowLayout

2-您应该实现collectionView(_:layout:sizeForItemAt:)方法,如下所示:

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)
}
Run Code Online (Sandbox Code Playgroud)

假设最小空格为 8 - 例如 -,

在此处输入图片说明

输出应该是每行三个项目,正方形大小。

备注:如果要将单元格之间的空格设置为零,scaleFactor则应为:

let scaleFactor = (screenWidth / 3)
Run Code Online (Sandbox Code Playgroud)

希望这有帮助。


Sta*_*man 6

你可以用这个

这段代码以某种方式编写,您可以更改节插入或minimumInteritemSpacing,并使用此参数计算和调整大小

您可以使用此代码或从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)
    }

Run Code Online (Sandbox Code Playgroud)


Dev*_*ami 5

你必须实施

UICollectionViewDelegateFlowLayout 用于间距的东西。

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
}
Run Code Online (Sandbox Code Playgroud)