swift 如何消除UICollectionView的表头和单元格之间差距?

pjngdqdw  于 2023-05-05  发布在  Swift
关注(0)|答案(7)|浏览(155)

我有一个UICollectionView,它有一个标题和一个单元格。我想删除标题和单元格之间差距。如何在swift中实现?
这是我的观点……

我添加了背景颜色的collectionView和标题和细胞也..
请看截图。

wqsoz72f

wqsoz72f1#

使用UICollectionViewFlowLayout的属性section Inset

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 10, right: 10)
// Here you can set according your requirement

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

ryevplcw

ryevplcw2#

使用UICollectionViewFlowLayout的属性sectionInset

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

layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 1;

在Swift中:

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

jjjwad0x3#

附加脚本屏幕的屏幕截图。按照你的故事板设置并检查。希望你的问题能得到解决。

yshpjwxd

yshpjwxd4#

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

9cbw7uwe

9cbw7uwe5#

试试这个:

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))
    }
vaj7vani

vaj7vani6#

下面是我的示例代码,你必须设置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
}()
k4ymrczo

k4ymrczo7#

自定义UIEdgeInsetsMake:

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

相关问题