如何在UICollectionView中更改单元格之间的空间?

时间:2016-02-26 14:20:39

标签: ios swift uicollectionview uicollectionviewcell

我想减少行中单元格之间的大小。 现在看起来像:

enter image description here

我正在尝试这个,以减小它们之间的大小:

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 2, bottom: 10, right: 2)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0

但它对我没有帮助。我做错了什么?

8 个答案:

答案 0 :(得分:24)

//Objective c
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
        return CGSizeMake((collectionView.frame.size.width/3)-20, 100);
    }

// Swift 2.0

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSizeMake((collectionView.frame.size.width / 3) - 20, 100)
}

// Swift 3.0
override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: CGFloat((collectionView.frame.size.width / 3) - 20), height: CGFloat(100))
}

答案 1 :(得分:23)

转到故事板,右键单击集合视图和enter image description here

检查单元格之间的最小间距,并将其减小到零。

答案 2 :(得分:9)

在您的代码中,您必须为集合视图创建一个IBOutlet,然后分配collectionViewLayout:

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 2, bottom: 10, right: 2)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
collectionView!.collectionViewLayout = layout

答案 3 :(得分:3)

简单

let layout = contactsCollectionView.collectionViewLayout as? UICollectionViewFlowLayout
layout?.minimumLineSpacing = 8

答案 4 :(得分:1)

如果您在CollectionViewStoryBoard中创建Xib,请转到StoryBoardXib并从

设置这些值

enter image description here

答案 5 :(得分:0)

扩展ViewController:UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let collectionWidth = collectionView.bounds.width
    return CGSize(width: collectionWidth, height: collectionWidth/2)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 100
}

}

答案 6 :(得分:0)

以编程方式快速5

lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal

        //Provide Width and Height According to your need
        let width = UIScreen.main.bounds.width / 4
        let height = UIScreen.main.bounds.height / 10
        layout.itemSize = CGSize(width: width, height: height)

        //Make Space as Zero
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0

        return UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    }()

您可以在lazy collectionView块中提供更多自定义内容。

答案 7 :(得分:0)

(快速4)

  • 单击情节提要中的收藏集视图。
  • 在右侧,单击尺寸检查器。
  • 管理并更改像元大小值,您将自己弄清楚。