如何使UICollectionViewCell的高度填充框架的所有大小?

时间:2015-12-09 17:08:44

标签: ios uicollectionview uicollectionviewcell

我正在尝试使用CollectionViewCell来填充CollectionView的所有帧,但是我无法完成此操作,相反,它看起来像这样: How it currently looks

如您所见,uicollectionview框架的顶部和底部有一个空格,不应出现。

配置如下:

//////////////////////////////
    //     Collection View      //
    //////////////////////////////
    self.collectionView.delegate = self
    self.collectionView.dataSource = self

    self.automaticallyAdjustsScrollViewInsets = false

    let screenWidth = UIScreen.mainScreen().bounds.width
    let viewHeight = viewContainer.frame.height

    let collectionViewFlowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
    collectionViewFlowLayout.itemSize = CGSizeMake(screenWidth, viewHeight)
    collectionViewFlowLayout.minimumInteritemSpacing = 0.0
    collectionViewFlowLayout.minimumLineSpacing = 0.0
    collectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)

如何删除这些不需要的空格?

2 个答案:

答案 0 :(得分:2)

覆盖UICollectionViewFlowLayout Delegate方法并返回大小等于collectionview大小:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
       return collectionView.frame.size;
}

答案 1 :(得分:2)

根据您设置collectionViewFlowLayout属性的位置,您可能需要致电invalidateLayout。我建议在视图控制器上采用UICollectionViewDelegateFlowLayout并实现方法:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return self.collectionView.bounds.size
    }

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

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