I have a UICollectionView created from storyboard,
I want to have 3 items per row in the view. I managed to do that using the following:
- (CGSiz
(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
float cellWidth = screenWidth / 3.2; //Replace the divisor with the column count requirement. Make sure to have it in float.
CGFloat screenHeight = screenRect.size.height;
float cellHeight = screenHeight/3.0;
CGSize size = CGSizeMake(cellWidth, cellHeight);
return size;
}
Apply the below code too-(viewWillTransitionToSize)
(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[self.collectionView/*(that's my collection view name)*/ reloadData];
}
This Code will help to get the same no. of cells in portrait as well as in landscape mode also. I have applied above code to get 3 cells in portrait as well as 3 cells in landscape mode.