ios - UICollectionView 在单元格之间添加间隙

标签 ios objective-c iphone swift uicollectionview

我正在使用 ALAssetsLibrary 访问系统照片库以预览和多选照片,这类似于原始系统照片库,我可以滚动预览照片。我通过 实现UICollectionViewUICollectionViewFlowLayout。除小问题外一切顺利。

当我使用 iOS 系统照片库并滚动预览照片时,两张照片之间有一个间隙,如下所示。 enter image description here

虽然在我的应用程序中,滚动时两张照片之间没有间隙。我设置了 UICollectionView 分页和 UICollectionViewFlowLayout 单元格空间( minimumLineSpacingminimumInteritemSpacing) 都归零。 enter image description here

如何让我的照片预览像系统照片库一样,两张滚动照片之间有一个间隙?我是 UICollectionView 的新手,我不确定 UICollectionView 是否strong>Gap 由 SupplementaryView 或其他任何东西实现。

最佳答案

这可以在

  1. 将 UICollectionView 框架宽度设置为大于屏幕宽度。

    layout = [[UICollectionViewFlowLayout alloc]init];
    CGRect cSize = self.view.bounds;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    // 1. UICollectionView frame width lager than screen with => Screen width + 2*(required Space)
    cSize.size.width += 20; 
    self.collectionGallery = [[UICollectionView alloc]initWithFrame:cSize collectionViewLayout:layout];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    
  2. 设置 UIImageView 框架宽度等于屏幕宽度。

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    GalleryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    
    cell.imageGallery.contentMode = UIViewContentModeScaleAspectFit;
    // 2
    cell.imageGallery.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    
    return cell;
    }
    
  3. 单元格宽度应大于屏幕宽度,与步骤 1 相同。

    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeMake(self.view.frame.size.width + 20, self.view.frame.size.height); // cell size should be same as collectionview width
    return size;
    }
    

希望对您有所帮助。 如果这解决了您的问题,请发表评论并投票

关于ios - UICollectionView 在单元格之间添加间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37319216/

相关文章:

IOS swift 当键盘出现时它隐藏了前 2 个 TextField

ios - 将数据转换为字符串得到 nil 值

iphone - CGBitmapContextCreate的处理错误

iphone - 我必须注册下载我的应用程序的每个设备 token 吗?

ios - 在模拟器上运行,同时拥有不在模拟器上显示类的 SDK(阻止我运行)

javascript - 组件无法识别它正在导出

ios - 以编程方式从另一个应用程序下载内部应用程序

ios - 本地化 Info.plist 后出现构建错误

objective-c - 在我的 iphone 应用程序中,我想提供导入 Facebook、gmail 联系人的功能

iphone - 如何使用 UITableView 中的标题部分展开和折叠行