iOS-UICollectionView在设置为0后仍具有单元格之间的间距
当将所有内容设置为0时,单元格之间的间距很小。我在iOS-UICollectionView在设置为0后仍具有单元格之间的间距,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,当将所有内容设置为0时,单元格之间的间距很小。我在cellForItemAtIndexPath中所做的就是为每个单元格设置backgroundColor 这是我的密码: //collectionView frame _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150) collectionViewLayout:layout]; ...
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;
}
我的集合视图如下所示。你会看到一些单元格之间有黑线。这是什么原因造成的?我怎样才能去掉这些线
非常感谢您的帮助。提前感谢。因为宽度和高度都不能被12整除,所以它会保留一些空像素点。为了避免这种情况,collectionView的宽度和高度都应该是12的倍数。我也面临这个问题。感谢@tenric指出,对我来说是的,因为宽度不能被所需列数整除。我的解决办法如下:
var itemWidth: CGFloat {
get {
return floor(view.frame.size.width / 7)
}
}
因为我使用的是AutoLayout,所以我只需将间隙调整为collectionView的宽度,如下所示(请参阅widthAnchor):
我也为此发疯了。我想要一个通用的解决方案,因为我的iPhone每行有3个电池,iPad每行有4个电池,UICollectionView覆盖了整个屏幕的宽度,所有设备的宽度都不同 最后,我使用ceil(frame.width/itemsPerRow)使一行中单元格的组合宽度比设备屏幕宽。然后,我计算了一行中单元格的总宽度,并与屏幕宽度(view.frame)进行比较,得到单元格将产生的溢出宽度。知道溢出后,我将UICollectionView的尾部约束设置为负溢出。这意味着UICollectionView实际上会在屏幕宽度上溢出一个像素左右,但在我的例子中,这不是问题。希望下面的代码将有助于澄清并让您了解如何解决这个问题
// calculating cell width
let frame = view.frame
let cellWidth = ceil(frame.width / itemsPerRow)
// adjust UICollectionView to avoid space between cells
let totalCellsWidth = cellWidth * itemsPerRow
let overflow = totalCellsWidth - frame.width
let adjustedConstraint = -overflow
collectionViewsTrailingConstraint.constant = adjustedConstraint
我也面临这个问题。我相信这是因为autolayout中有一个bug。除非你做一些疯狂的破解,否则电池之间的1px间隙是无法消除的!!!您是否尝试过ceil(_collectionView.frame.size.width/12)?我确实将每个单元格的大小更改为26.66667(320/12),但这种情况仍在发生。但是谢谢你的回答。
// calculating cell width
let frame = view.frame
let cellWidth = ceil(frame.width / itemsPerRow)
// adjust UICollectionView to avoid space between cells
let totalCellsWidth = cellWidth * itemsPerRow
let overflow = totalCellsWidth - frame.width
let adjustedConstraint = -overflow
collectionViewsTrailingConstraint.constant = adjustedConstraint