2015-12-15 181 views
0

我試圖刪除UICollectionView之間的空間,這是從設備的截圖如何刪除UICollectionViewCells之間的空間?

enter image description here

我真的想避免這些空間(藍色)和我曾嘗試以下代碼

這是我的CollectionView代表

#pragma mark <UICollectionViewDataSource> 

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { 
    return 1; 
} 


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
    return 30; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor redColor]; 
    return cell; 
} 

首先我想這一次,它不工作

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init]; 
    flow.itemSize = CGSizeMake(cell.frame.size.width, cell.frame.size.height); 
    flow.scrollDirection = UICollectionViewScrollPositionCenteredVertically; 
    flow.minimumInteritemSpacing = 0; 
    flow.minimumLineSpacing = 0; 
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"]; 

} 

,我想這也是一個,它不工作

#pragma mark collection view cell paddings 
- (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; 
} 

請幫我解決這個問題。

+0

你應該使用方法sizeForItemAtIndexPath和佈局類 – Anton

+0

好吧讓我結帳 – Bangalore

+0

我只能看到快速的例子嗎? – Bangalore

回答

1

從故事板還可以設置間隔的任何值屬性。選擇您的收藏視圖 - 選擇尺寸檢查器選項卡,您可以調整最小間距代表代表細胞代表線條。

而且您還可以調整節插頁也。

enter image description here

+0

這是工作cool.thanks – Bangalore

+0

你應該注意到它是'最小'的間距,而不是固定的間距。在某些情況下,它可能比它大。如果遇到這種情況,請查看下面我的答案中的代碼,應該幫助 – Wingzero

1
flow.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1); 

使用sectionInsetUICollectionViewFlowLayoutviewdidload

+0

它不起作用, – Bangalore

+0

layout.sectionInset = UIEdgeInsetsMake(0,0,0,0); layout.itemSize = CGSizeMake((self.view.frame.size.width/2)+4,(self.view.frame.size.width/2)+4); 這是爲我工作。試一試。 –

+0

感謝您的回答 – Bangalore

2

所以有屬性和方法可以限制的最小空間,如minimumInteritemSpacingForSectionAtIndex

但是它僅僅是「最小空間」並不總是最大的空間。

這個我使用的是什麼:借用Cell spacing in UICollectionView

覆蓋標準流程佈局:

- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect { 
    NSArray *answer = [super layoutAttributesForElementsInRect:rect]; 

    for(int i = 1; i < [answer count]; ++i) { 
     UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i]; 
     UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1]; 
     NSInteger maximumSpacing = 4; 
     NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame); 

     if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) { 
      CGRect frame = currentLayoutAttributes.frame; 
      frame.origin.x = origin + maximumSpacing; 
      currentLayoutAttributes.frame = frame; 
     } 
    } 
    return answer; 
} 

maximumSpacing可以設置成你喜歡

+0

我應該在哪裏調用? – Bangalore

+0

你不叫它。您添加標準流佈局的子類,並將代碼放入佈局實現中。查看'UICollectionViewFlowLayout' – Wingzero

+0

在[super layoutAttributesForElementsInRect:rect]上出錯; – Bangalore

0

參見如何(使用自動版式約束)完全適合所有細胞,而不在它們之間的任何間隔來調整UICollectionView寬度my answer

+0

參考評論中的鏈接而不是答案。 – AsifAli72090