当将所有内容都设置为 0 时,单元格之间的空间变小了。我在 cellForItemAtIndexPath
中所做的就是为每个单元格设置 backgroundColor
。
这是我的代码:
//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;
}
我的收藏 View 看起来像这样。你会看到一些单元格之间有黑线。是什么原因造成的?如何删除线条?
非常感谢任何帮助。提前致谢。
最佳答案
我也遇到过这个问题。感谢@tenric 指出,对我来说是的是因为宽度不能被所需列数整除。我的解决方案如下:
var itemWidth: CGFloat {
get {
return floor(view.frame.size.width / 7)
}
}
因为我使用的是AutoLayout,所以我只是将间隙调整为collectionView的宽度如下(引用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 后单元格之间仍然有间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33872527/