IOS: automatic UICollectionViewCell size depending on screen size

I am trying to create 10 cells in a collection view (the same size as the screen). When I run my application in iphone5s simulator, the view contains exactly 5 cells. But when I switch to iphone6p simulator, the view contains more than 5 cells. How to adjust the cell size so that the number of cells on the screen is consistent in different screen sizes?

+5
source share
2 answers
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { int numberOfCellInRow = 3; CGFloat cellWidth = [[UIScreen mainScreen] bounds].size.width/numberOfCellInRow; return CGSizeMake(cellWidth, cellWidth); } 
+15
source
 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ return CGSizeMake([(NSString*)[[tagArray objectAtIndex:indexPath.row] objectForKey:@"tag"] sizeWithAttributes:NULL].width+10, 40); } 
0
source

All Articles