将细胞放置在中心的集合视图中而不是快速

时间:2015-11-04 22:24:48

标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout

默认情况下,集合视图将尝试通过将它们均匀间隔来隔开集合中的单元格,以便有一个单元格触及集合视图的任一侧。例: enter image description here

enter image description here

但是,当单元格太宽而无法在集合视图中的一行中放置多个单元格时,它会将单元格置于集合视图中。enter image description here

是否有办法使居中单元格的行为(如第3张图片中)成为集合视图的默认值,以便各个单元格之间有0个空格,并且集合视图边缘的空间之间有空格?

2 个答案:

答案 0 :(得分:0)

我的回答是使用精心设置的东西,如

let insets = UIEdgeInsets(top: yourvalues, left: yourvalues, bottom: yourvalues, right: yourvalues)

并将其提供给YourCollectionView.contentInset

func collectionView(collectionView: UICollectionView,
    layout collectionViewLayout: UICollectionViewLayout,
    insetForSectionAtIndex section: Int) -> UIEdgeInsets {
      return sectionInsets
  }

这应该有效! Lemme知道你是否有任何麻烦!

答案 1 :(得分:0)

不是我意识到的。你应该实现这样的东西:

func collectionView(_ collectionView: UICollectionView,
                  layout collectionViewLayout: UICollectionViewLayout,
  sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    CGFloat width = UIScreen.mainScreen().bounds.size.width
    return CGSize(width, anyHeight)
}

然后在下面实现委托方法:

func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    return UIEdgeInsetsZero
}

如果适用,请确保实施旋转处理。

相关问题