0

I'm trying to build a UICollectionView Layout, that will adjust the size of the UICollectionView cells depending how many are they, so they could fit in a specific UICollectionView frame. For instance, if there is just one cell it's size will be (50,50) and vertically centered in the UICollectionView. If there is two cells, its size would be something like (48,48) also vertically centered in the UICollectionView frame, if there are 3 cells their size would be something like (45,45) also vertically centered, and so on, always respecting the boundaries of the UICollectionView frame. So it there is 20 cells, it's size would be something very small like (5,5) and they will all fit in the collectionview frame. To have a more clear idea, you can have a look to this app as an example, https://itunes.apple.com/es/app/pro-metronome-tempo-keeping/id477960671?mt=8, when you are adding beats the cells become smaller but they always fit in the frame.

I started with this something like this:

- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {

float leftInset = (collectionView.frame.size.width - (CELL_COUNT*50 + CELL_COUNT-1 * 10)) / 2;

//This kind of work for vertically centering the cells, but it doesn't respect the frame of the UICollectionView so the cells go outside of the boundaries.

return UIEdgeInsetsMake(0, leftInset, 0, 0); // top, left, bottom, right

}

Any idea how to do this?

Pau Senabre
  • 4,155
  • 2
  • 27
  • 36

0 Answers0