[英]UICollectionView add gap between cells
我正在使用ALAssetsLibrary
訪問系統照片庫進行預覽和多個選擇照片,這就像原始系統照片庫,我可以滾動預覽照片。我通過UICollectionView
和UICollectionViewFlowLayout
實現它。除了一點點問題外, UICollectionViewFlowLayout
順利。
當我使用iOS系統照片庫並滾動預覽照片時,兩張照片之間有一個間隙 ,如下所示。
在我的應用程序中,滾動時兩張照片之間沒有間隙 。我將UICollectionView
分頁和UICollectionViewFlowLayout
單元格空間( minimumLineSpacing
和minimumInteritemSpacing
)設置為零。
如何使用兩張滾動照片之間的間隙使我的照片預覽像系統照片庫?我是UICollectionView
,我不確定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; }
希望這會幫助你。 如果這可以解決您的問題,請做出評論並投票
您可以設置屬性.itemSize = CGSizeMake(desiredWidth, desiredHeight);
您的流程布局到圖像頁面所需的大小。
在此之后, .minimumInteritemSpacing
將正常工作。
要在預覽和集合之間進行更改,請使用兩個FlowLayout,一個有間隙,一個沒有間隙。
最小間距和截面插入將為您完成工作。
不要這樣做 - >單元格空間(minimumLineSpacing和minimumInteritemSpacing)為零。
這個UICollectionViewFlowLayoutDelegate方法將幫助你..
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 10; // This is the minimum inter item spacing, can be more
}
如果您需要更多空間來排列單元格,還可以調整edgeInsets
使用下面的代碼,如果使用miniumInteritemSpacingForSectionAtIndex:
每個項目中5px之間的間距,你需要邊距,在insetForSectionAtIndex:
設置此方法的值top,left,bottom,right insetForSectionAtIndex:
- (UIEdgeInsets)collectionView:(UICollectionView *) collectionView
layout:(UICollectionViewLayout *) collectionViewLayout
insetForSectionAtIndex:(NSInteger) section {
return UIEdgeInsetsMake(0, 0, 0, 0); // top, left, bottom, right
}
- (CGFloat)collectionView:(UICollectionView *) collectionView
layout:(UICollectionViewLayout *) collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger) section {
return 5.0;
}
希望它有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.