iOS - UICollectionView 设置为 0 后,单元格之间仍有间距 [英] iOS - UICollectionView still have spacing between cells after set to 0
问题描述
将所有内容设置为 0 时,单元格之间的间距很小.我在 cellForItemAtIndexPath
中所做的只是为每个单元格设置 backgroundColor
.
Im getting small spaces between cells when setting everything to 0. All I did in the cellForItemAtIndexPath
is setting backgroundColor
for each cell.
这是我的代码:
//collectionView frame
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150) collectionViewLayout:layout];
...
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//The cell sizes are divided by the width of the collectionView.
return CGSizeMake(_collectionView.frame.size.width/12, _collectionView.frame.size.width/12);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 0.0f;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 0.0f;
}
我的收藏视图如下所示.您会看到某些单元格之间有黑线.这是什么原因造成的?如何去除线条?
My collection view looks like this. You'll see that there are black lines between some cells. What is causing this? How can I remove the lines?
非常感谢任何帮助.提前致谢.
Any help is greatly appreciated. Thanks in advance.
推荐答案
我也遇到了这个问题.感谢@tenric 指出,对我来说是的,因为宽度不能被所需的列数整除.我的解决方案如下:
I too faced this problem. Thanks to @tenric for pointing out and yes for me was because the width is not divisible by the number of desired columns. My solution as follow:
var itemWidth: CGFloat {
get {
return floor(view.frame.size.width / 7)
}
}
并且因为我使用的是 AutoLayout,所以我只是将间隙调整为 collectionView 的宽度如下(参考 widthAnchor):
And because I'm using AutoLayout, I just adjust the gap to the width of the collectionView as follow (refer to widthAnchor):
collectionView?.topAnchor.constraint(equalTo: headerSeparator.bottomAnchor).isActive = true
collectionView?.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
collectionView?.bottomAnchor.constraint(equalTo: footerSeparator.topAnchor).isActive = true
let gap = view.frame.size.width - (itemWidth * 7)
collectionViewWidthConstraint = collectionView?.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -gap)
这篇关于iOS - UICollectionView 设置为 0 后,单元格之间仍有间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!