我正在使用 ALAssetsLibrary
访问系统照片库以预览和多选照片,这类似于原始系统照片库,我可以滚动预览照片。我通过 实现UICollectionView
和 UICollectionViewFlowLayout
。除小问题外一切顺利。
当我使用 iOS 系统照片库并滚动预览照片时,两张照片之间有一个间隙,如下所示。
虽然在我的应用程序中,滚动时两张照片之间没有间隙。我设置了 UICollectionView
分页和 UICollectionViewFlowLayout
单元格空间( minimumLineSpacing
和 minimumInteritemSpacing
) 都归零。
如何让我的照片预览像系统照片库一样,两张滚动照片之间有一个间隙?我是 UICollectionView
的新手,我不确定 UICollectionView
是否strong>Gap 由 SupplementaryView
或其他任何东西实现。
最佳答案
这可以在
将 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;
设置 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; }
单元格宽度应大于屏幕宽度,与步骤 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/