减少集合视图中单元格之间的空间

时间:2015-04-15 12:02:01

标签: ios objective-c

cells collectionView之间的空间很大,如下图所示。我怎样才能减少它。

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

    NSLog(@"SETTING SIZE FOR ITEM AT INDEX %ld", (long)indexPath.row);
    CGSize mElementSize = CGSizeMake(100, 50);
    return mElementSize;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 2.0;
}

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

// Layout: Set Edges
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // return UIEdgeInsetsMake(0,8,0,8);  // top, left, bottom, right
    return UIEdgeInsetsMake(0,0,0,0);  // top, left, bottom, right
}


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

enter image description here

更新

enter code here

更新

enter image description here

3 个答案:

答案 0 :(得分:0)

您应该能够使用身份检查器直接从Xib调整空间,调整" Min Spacing"或"部分插入"帮助

enter image description here

答案 1 :(得分:0)

您可以根据屏幕尺寸调整UICollectionViewCell的尺寸,以缩小两个UICollectionViewCells之间的差距。

更新1

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
      return CGSizeMake([UIScreen mainScreen].bounds.size.width/4, 300);
      // A total of 4 views displayed at a time,  we divide width / 4, 
      // and cell will automatically adjust its size.
}

HTH享受编码!!

答案 2 :(得分:0)

为collectionView flowlayout创建自定义类,并使用自定义类

在IB中设置flowlayout
@interface NDCollectionViewFlowLayout : UICollectionViewFlowLayout

@end

//由Nick Snyder于11/13/12创建。 // https://gist.github.com/nicksnyder/4075682 // UICollectionView flowLayout not wrapping cells correctly (iOS) // NDCollectionViewFlowLayout.m

#import "NDCollectionViewFlowLayout.h"

@implementation NDCollectionViewFlowLayout

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
  NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
  NSMutableArray *newAttributes = [NSMutableArray arrayWithCapacity:attributes.count];
  for (UICollectionViewLayoutAttributes *attribute in attributes) {
    if ((attribute.frame.origin.x + attribute.frame.size.width <= self.collectionViewContentSize.width) &&
        (attribute.frame.origin.y + attribute.frame.size.height <= self.collectionViewContentSize.height)) {
      [newAttributes addObject:attribute];
    }
  }
  return newAttributes;
}

@end

enter image description here enter image description here