How to remove spacing in collectionview cells. I want to get 18 rows and 22 columns which should fit according to frame height and width. I override the UICollectionViewFlowLayout but there is a space after last column in collection view. Can some one please help me to solve this.
Asked
Active
Viewed 576 times
3
-
Height is independent here. For width You need to manage it. Give proper width & Space according to your requirement. – Vishal Sharma May 09 '16 at 08:35
-
i take screen width and divide it by 22. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {float cellWidth = screenWidth / [self.advncdObj.motionGridX integerValue]; CGSize size = CGSizeMake(cellWidth, cellHeight);} – user1728987 May 09 '16 at 08:54
3 Answers
1
There are two ways to do it----
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Collection view
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 30;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MagazineCell *mCell = (MagazineCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
mCell.backgroundColor = [UIColor lightGrayColor];
return mCell;
}
#pragma mark Collection view layout things
// Layout: Set cell size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"SETTING SIZE FOR ITEM AT INDEX %d", indexPath.row);
CGSize mElementSize = CGSizeMake(104, 104);
return mElementSize;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 0.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
}
@end
And the other way--------> 1.Override standard flow layout. 2.Add implementation like that:
- (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 = 0;
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;
}

raj
- 63
- 5
-
I override and used the above method but in this case collection view is not fitting to the device width instead a small space after collection view – user1728987 May 09 '16 at 09:49
-
1in storyboard- collection view size, have you selected minimum spacing for cells as 0.0 ?? – raj May 09 '16 at 09:55
-
-
can you edit your question and add screenshot of what exactly is happening??!! – raj May 09 '16 at 09:59
-
1try my first answer and return 0 for minimumInteritemSpacingForSectionAtIndex. and adjust your other requirements accordingly – raj May 09 '16 at 10:09
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111405/discussion-between-raj-and-user1728987). – raj May 09 '16 at 10:23
0
UICollectionViewFlowLayoutDelegate
method will help you..
Option 1:
- (CGFloat)collectionView:(UICollectionView *)collectionView layout: (UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 0; // This is the minimum inter item spacing, can be more
}
Option 2:
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
flow.itemSize = CGSizeMake(cellWidth, cellHeight);
flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flow.minimumInteritemSpacing = 0;
flow.minimumLineSpacing = 0;
[mainCollectionView reloadData];
mainCollectionView.collectionViewLayout = flow;
Last line is important where we are assigning the layout
if you need more space to arrange your cells adjust the edgeInsets also.and also you can refer this link iOS - UICollectionView spacing still there when set to 0 - How to set with no spacing between cells

Community
- 1
- 1

Shangari C
- 792
- 1
- 5
- 17
0
#pragma mark Collection View Delegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 330;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(Screen_Width/22, Screen_Width/22);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 0,0, 0);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
customcell *cell = (customcell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"customcell" forIndexPath:indexPath];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"customcell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
return cell;
}

Monika Patel
- 2,287
- 3
- 20
- 45