如何消除UICollectionView的标题和单元格之间的差距?

时间:2016-05-06 05:23:25

标签: ios swift uicollectionview

我有一个UICollectionView,它有一个标题和一个单元格。 我想删除标题和单元格之间的差距.. 如何在swift

中执行此操作

以下是我的观点......

enter image description here

我为collectionView和标题和单元格添加了背景颜色..

请参阅截图。

enter image description here

7 个答案:

答案 0 :(得分:3)

使用UICollectionViewFlowLayout

的属性部分插入
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 10, right: 10)
// Here you can set according your requirement

enter image description here

更多参考:http://www.brianjcoleman.com/tutorial-collection-view-using-swift/

答案 1 :(得分:1)

使用sectionInset的属性UICollectionViewFlowLayout

UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 1;
在Swift中

var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 1

答案 2 :(得分:0)

附加故事板屏幕截图。根据您的故事板设置并检查。希望你的问题能够得到解决。

enter image description here

答案 3 :(得分:0)

我忘了将插入部分设置为零。 它解决了我的问题。 谢谢大家。

答案 4 :(得分:0)

试试这个:

   override func viewWillAppear(animated: Bool) {
self.automaticallyAdjustsScrollViewInsets = false;
}

在UICollectionViewDelegate上:

 let k_cViewPadding = 8.0

func collectionView(collectionView: UICollectionView,
        layout collectionViewLayout: UICollectionViewLayout,
        sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
            let cellDiffference:CGFloat = CGFloat(k_cViewPadding*4)
            return(CGSizeMake((collectionView.frame.size.width-cellDiffference)/3, (collectionView.frame.size.width-cellDiffference)/3))
    } 

答案 5 :(得分:0)

这是我的示例代码,您必须为间距设置 UIEdgeInsets

private let collectionViewLayout: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = UICollectionView.ScrollDirection.horizontal
    layout.sectionInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    collectionView.translatesAutoresizingMaskIntoConstraints = false
    collectionView.showsHorizontalScrollIndicator = false
    collectionView.showsVerticalScrollIndicator = false
    collectionView.register(CustomCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
    collectionView.register(TopExpertCustomCell.self, forCellWithReuseIdentifier: "TopExpertCollectionViewCell")
    collectionView.backgroundColor = UIColor(hex: "#F1F1F1")
    return collectionView
}()

答案 6 :(得分:0)

自定义您的 UIEdgeInsetsMake:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0,0,0,0);  //{top, left, bottom, right};
}