0

I am having trouble creating a perfect 3x3 grid using a UICollectionView on the iPhone 5. Basically I want there to be 0 spacing between each cell. When I do it on the iPhone 6, it works because the width can be divided perfectly into 3 (375px/3 = 125px). However, with the iPhone 5, 320px does not divide into 3 perfect.

Therefore on the iPhone 5, I get little lines like this: enter image description here

But as you can see, it's fine on the iPhone 6:

enter image description here

Is there a solution to this or is it just impossible?

Here is my setup:

- (void)viewDidLoad {
    [super viewDidLoad];

    UICollectionViewFlowLayout *feedLayout = [[UICollectionViewFlowLayout alloc] init];
    //feedLayout.headerReferenceSize = CGSizeMake([self.view frameSizeWidth],235);

    self.profileCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, navigationbarHeight + [_statsHeader frameSizeHeight], self.view.frameSizeWidth, self.view.frameSizeHeight - navigationbarHeight - [_statsHeader frameSizeHeight] - tabBarHeight)  collectionViewLayout:feedLayout];
    [self.profileCollectionView setDataSource:self];
    [self.profileCollectionView setDelegate:self];
    [self.profileCollectionView registerClass:[ANListenFeedCollectionViewCell class] forCellWithReuseIdentifier:@"feedCell"];
    [self.profileCollectionView setBackgroundColor:[UIColor whiteColor]];

    [self.view addSubview:self.profileCollectionView];
    self.automaticallyAdjustsScrollViewInsets = NO;

}

//****************************************
//****************************************
#pragma mark - UICollectionViewDelegate
//****************************************
//****************************************


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

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (ANListenFeedCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    ANListenFeedCollectionViewCell *cell = (ANListenFeedCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"feedCell" forIndexPath:indexPath];
    cell.backgroundColor=[UIColor blackColor];
    return cell;
}


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

    return CGSizeMake([self.view frameSizeWidth]/3.0f, [self.view frameSizeWidth]/3.0f);
}

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

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
Vinodh
  • 5,262
  • 4
  • 38
  • 68
evenodd
  • 2,026
  • 4
  • 26
  • 38
  • "floor" the size after division to avoid subpixel rendering. Even if there is going to be 1 pixel left in the end, you'll barely notice that. And if you do, solution below is for you. – pronebird Dec 15 '14 at 22:09

1 Answers1

0

Ah, I figured out a work around. Make the middle box slightly smaller in width so that it fits. It's not perfect, but oh well.

if(indexPath.row % 3 == 0){
    return CGSizeMake(107, 107);
}
if(indexPath.row % 3 == 1){
    return CGSizeMake(106, 107);
}
if(indexPath.row % 3 == 2){
    return CGSizeMake(107, 107);
}
evenodd
  • 2,026
  • 4
  • 26
  • 38