Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: UICollectionViewCell auto adjust size according to screen size

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

like image 403
Guangyu Wang Avatar asked May 06 '15 11:05

Guangyu Wang


1 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);
}
like image 74
Britto Thomas Avatar answered Oct 24 '22 11:10

Britto Thomas