ios - 如何在单行中的两个单元格之间设置没有间隙的 Collection View 单元格

标签 ios objective-c uicollectionview

我使用 Storyboard放置了一个带有一个自定义单元格的 Collection View 。我总共有 6 个图像,所以每行我有 2 个图像(2 个单元格)。现在当我运行 2 个单元格之间的间隙时更多。我需要消除所有之间的所有间隙单元格,所以它应该看起来像没有间隙,有 6 个图像。现在下面的图像是我需要删除间隙 enter image description here

我尝试使用 Storyboard增加单元格的宽度。但我不工作。有什么想法可以让所有行中的每个单元格之间没有间隙(所有单元格之间的右、上、下间隙)

请帮帮我。谢谢

但是当我在 iphone 6 上运行时,我会变成这样,为什么?
enter image description here

最佳答案

使您的类符合 UICollectionViewDelegateFlowLayout 协议(protocol)并实现方法:

- (CGSize)collectionView:(UICollectionView *)collectionView
              layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    CGFloat width = self.collectionView.frame.size.width/2;
    return CGSizeMake(width, width);
}


-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {

   return UIEdgeInsetsZero;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {

   return 0.0;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
   return 0.0;
}

关于ios - 如何在单行中的两个单元格之间设置没有间隙的 Collection View 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35730392/

相关文章:

ios - 在 iPhone 上播放直播视频

ios - 是否可以删除/创建约束

iphone - 社交框架——如何知道消息是否发送

ios - 将字符串数组转换为连接字符串数组

ios - 将整数从 View Controller 传递给应用程序委托(delegate)

ios - tableview ios 中的多列

ios - 如何创建一个 segue 以从 TableView 推回编辑模式?

objective-c - 单击 UIButton 以编程方式创建 Segue 或推送到另一个 View Controller

ios - 在 scrollToItemAtIndexPath 之后获取 UICollectionView 单元格位置

IOS Swift 表格 View : Is there a way to move first cell one row down