gpt4 book ai didi

ios - swift : The spacing between my cells in my UICollectionView is not zero

转载 作者:行者123 更新时间:2023-11-28 21:42:46 24 4
gpt4 key购买 nike

虽然我有自己的自定义布局,但我的单元格仍然有一点间距。我试图在每行中放置 7 个项目,但我总是得到 6 个带有一堆间距的项目。

这是我的布局:

class monthFlow: UICollectionViewFlowLayout, UICollectionViewDelegateFlowLayout {
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width/7, height: collectionView.frame.size.height/8)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 0
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 0
}

但它看起来像这样:enter image description here

最佳答案

这很正常。 UICollectionViewFlowLayout 的内置行为是将单元格均匀地分布在屏幕上(完全合理)。如果你不想要那样,你将不得不做比你在 UICollectionViewFlowLayout 子类中做的更多的事情来改变行为 - 你需要覆盖 layoutAttributesForElementsInRect:layoutAttributesForItemAtIndexPath:

事实上,从您目前展示的代码来看,一开始就没有必要继承 UICollectionViewFlowLayout; 所做的一切都可以通过设置布局属性来完成。但是要将完全对齐更改为左对齐,您确实需要子类化。

您甚至可以考虑编写自己的 Collection View 布局不是作为 UICollectionViewFlowLayout 的子类,特别是因为月布局不是流式布局 - 它总是由 7 个单元组成,这不是流布局确实如此。因此,在某种程度上,您在这里的错误是认为您可以做到这一点而无需首先编写自己的布局。

关于ios - swift : The spacing between my cells in my UICollectionView is not zero,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31394900/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com