0

I am using third party UiCollection View KRLCollectionViewGridLayout.
In this I have the following issue attached in screen shot.

enter image description here

See the above screen shot. There is an extra space in collectionview cell.
My Question is how to remove these extra space in collection cell and also how to add separator between two cell?

Here is my Code:

- (KRLCollectionViewGridLayout *)layout
{
    return (id)self.collectionView.collectionViewLayout;
}

- (IBAction)changeColumnsTapped:(id)sender {
    [[[UIActionSheet alloc] initWithTitle:@"Choose how many columns"
                                 delegate:self
                        cancelButtonTitle:nil
                   destructiveButtonTitle:nil
                        otherButtonTitles:@"1",@"2", nil]
     showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    self.layout.numberOfItemsPerLine = [[actionSheet buttonTitleAtIndex:buttonIndex] integerValue];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, -44, self.view.frame.size.width, 44.0)];
    [self.collectionView addSubview:searchBar];
    [self.collectionView setContentInset:UIEdgeInsetsMake(44, 0, 0, 0)];

    self.layout.numberOfItemsPerLine = 1;
    self.layout.aspectRatio = 1;
    self.layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.layout.interitemSpacing = 10;
    self.layout.lineSpacing = 10;

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;

    // Register cell classes
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

    //recipePhotos = [NSMutableArray arrayWithObjects:@"image1.png",@"image2.png",@"image3.png", nil];
     recipePhotos = [NSMutableArray arrayWithObjects:@"image1.png", nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [recipePhotos count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell1" forIndexPath:indexPath];

    //cell.contentView.backgroundColor = [UIColor blueColor];
    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:[recipePhotos objectAtIndex:indexPath.row]];

    cell.layer.borderWidth = 0.5;

    return cell;
}
Mihir Oza
  • 2,768
  • 3
  • 35
  • 61

3 Answers3

1

I'm the author of this layout. As it stands now, it looks like you're using the default value of the layout's aspectRatio property of 1/1 which is what is in charge of sizing the cells. Unfortunately this layout does not yet support dynamically-sized cells, but I may do so in the future. If you know how high your cells are intended to be relative to their width, you can set the aspect ratio accordingly. an aspect ratio of 2 would mean it's twice as long as it is tall, and an aspect ratio of 0.5 would mean it's 1/2 as long as it is tall. You probably want some number greater than 1 for your ratio to decrease the spacing.

If that doesn't give you the effect you want, you may need to switch to UICollectionViewFlowLayout instead which gives you precise control over the size of cells.

Kevlar
  • 8,804
  • 9
  • 55
  • 81
0

Try This. Check This Link Cell spacing in UICollectionView

- (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
}
Community
  • 1
  • 1
Shruti
  • 1,849
  • 1
  • 13
  • 21
0

Add UICollectionViewDelegateFlowLayout delegate in you interface and ipmplement

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return IPAD ? CGSizeMake(75, 75) : CGSizeMake(55, 55);
}

And change size as per your requirement.

Samir
  • 902
  • 9
  • 23