evenodd evenodd - 1 month ago 7
iOS Question

UICollectionView can't create perfect 3x3 grid for iPhone 5. Is it possible?

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;
}

Answer

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);
}