删除UICollectionView底部的插入间距

时间:2017-08-27 21:42:13

标签: ios cocoa-touch uicollectionview uikit

我真的需要一些帮助。我已经尝试了几天但似乎无法解决它......

我正在尝试以UICollectionView网格样式布局图像,并禁用滚动。所以我有4个图像/单元格,我试图用这些图像完全填充UICollectionView,它们之间有1个间距。

问题,但是单元格之间的间距在中心和底部之间分开。

我注意到,当将UICollectionView的scrollDirection从垂直更改为水平时,问题仍然存在,但也只是改变了方向,所以我猜它与此有关。

示例:

代码:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        if isInFeed && self.postImages!.count > 1 {

            guard let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout else {
                return CGSize()
            }
            flowLayout.minimumInteritemSpacing = 1
            flowLayout.minimumLineSpacing = 1

            switch (self.postImages!.count) {

            case 2:
                // we split the collectionView into 2 parts (+ 2*0.5 spacing)
                return CGSize(width: (self.collectionView!.bounds.width/2)-CGFloat(0.5), height: self.collectionView!.bounds.height)

            case 3:

                // we split the collectionView into 3 parts, the first one taking up half, the other 2 images taking up 1/4th (+spacing)
                if indexPath.row == 0 { return CGSize(width: (self.collectionView!.bounds.width/2)-CGFloat(0.5), height: self.collectionView!.bounds.height) }
                else { return CGSize(width: (self.collectionView!.bounds.width/2)-CGFloat(0.5), height: (self.collectionView!.bounds.height/2)-CGFloat(0.5)) }

            case 4:
            // we split the collectionView into 4 parts (1/4th + spacing)


//                if indexPath.section == 0 { return CGSize(width: (self.collectionView!.bounds.width/2)-CGFloat(0.5), height: (self.collectionView!.bounds.height/2)-CGFloat(1))}
//                else {return CGSize(width: (self.collectionView!.bounds.width/2)-CGFloat(0.5), height: (self.collectionView!.bounds.height/2)-CGFloat(0))}

                return CGSize(width: (self.collectionView!.bounds.width/2)-CGFloat(0.5), height: (self.collectionView!.bounds.height/2)-CGFloat(0.5))

            default: break
            }
        }
        // If not in Feed or just one image, take up entire collectionView
        return CGSize(width: self.collectionView!.bounds.width, height: self.collectionView!.bounds.height)
    }

我还尝试了对UICollectionViewLayoutUICollectionViewFlowLayout进行子类化,但没有成功,我已经阅读了Apple的文档,但这似乎没什么帮助。

希望这能清楚地解释问题,我很乐意详细说明。 祝你有美好的一天!

修改:

我将滚动方向从垂直方向更改为水平方向,并为每个单元格添加了2个额外的计算间距,以显示如果我这样做会发生什么。

example2

编辑2:

终于设法解决了! (尽管不像我想的那么干净,现在确实有效) 我将以下代码放在collectionViewLayout方法中:

if indexPath.section == 0 {
     flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 1 * UIScreen.main.scale)
 } else {
     flowLayout.sectionInset = UIEdgeInsets.zero
 }

非常感谢大家试图帮忙!

4 个答案:

答案 0 :(得分:2)

对我有用的最终解决方案(适用于遇到类似情况的任何人):

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

    if section == 0 {
        return UIEdgeInsetsMake(0, 0, 0, 1 * UIScreen.main.scale)
    } else {
        return UIEdgeInsets.zero
    }
}

答案 1 :(得分:1)

试试这段代码。添加一个函数并在视图中调用确实加载。你可能需要做一些调整,但它适用于我的情况。

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    let width = UIScreen.main.bounds.width
    layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    layout.itemSize = CGSize(width: width / 2, height: width / 2)
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    collectionView!.collectionViewLayout = layout

答案 2 :(得分:0)

在Storyboard中尝试使用Size Inspector,最小间距为零,所有Section Insects为零。

enter image description here

答案 3 :(得分:0)

layout.minimumLineSpacing = 1.0f;
layout.minimumInteritemSpacing = 1.0f;

CGSize collectionSize = collectionView.frame.size;
CGFloat cellsPerRow = 2.0f;
CGFloat spacing = 0.5f;

CGFloat itemWidth = collectionSize.width/cellsPerRow - spacing;
CGFloat itemHeight = collectionSize.height/cellsPerRow - spacing;

return CGSizeMake(itemWidth,itemHeight);

enter image description here