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
You need to calculate first the available space for the content and then divide that by the number of items you want to present per row.
Here is an example assuming that the UICollectionView
is taking all the width of the screen
private let numberOfItemPerRow = 2
private let screenWidth = UIScreen.main.bounds.width
private let sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
private let minimumInteritemSpacing = CGFloat(10)
private let minimumLineSpacing = CGFloat(10)
// Calculate the item size based on the configuration above
private var itemSize: CGSize {
let interitemSpacesCount = numberOfItemPerRow - 1
let interitemSpacingPerRow = minimumInteritemSpacing * CGFloat(interitemSpacesCount)
let rowContentWidth = screenWidth - sectionInset.right - sectionInset.left - interitemSpacingPerRow
let width = rowContentWidth / CGFloat(numberOfItemPerRow)
let height = width // feel free to change the height to whatever you want
return CGSize(width: width, height: height)
}
I know it is so late, but the solution that i am currently using and handled this case was by using KRLCollectionViewGridLayout as below
KRLCollectionViewGridLayout* aFlowLayout = [[KRLCollectionViewGridLayout alloc]init];
aFlowLayout.aspectRatio = 1;
aFlowLayout.sectionInset = UIEdgeInsetsMake(2, 2, 2, 2);
aFlowLayout.interitemSpacing = 2;
aFlowLayout.lineSpacing = 2;
aFlowLayout.numberOfItemsPerLine = 3;
aFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.heigh) collectionViewLayout:aFlowLayout];
You can find KRLCollectionViewGridLayout library class using this link
here i did it , i implemented UICollectionViewDelegateFlowLayout on UICollectionView add following method . it work , use it .
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
float cellWidth = screenWidth / 3.0; //Replace the divisor with the column count requirement. Make sure to have it in float.
CGSize size = CGSizeMake(cellWidth, cellWidth);
return size;
}
this method work as screen size width divided by 3 .
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(self.collectionView.bounds.size.width/3, self.collectionView.bounds.size.width/3);
}
Most important thing need to do as well is in size inspector of collectionView set up the minimum spacing for cell & line to 0
try this
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(self.view.frame.size.width/3 -10 , (collectionView.frame.size.height-100)/2);
}
UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout*)collectionView.collectionViewLayout;
flowLayout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
NSInteger itemRowCount = 3;
CGFloat rowWidth = collectionView.contentSize.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right;
CGFloat itemWidth = (rowWidth - flowLayout.minimumInteritemSpacing * (itemRowCount - 1) ) / itemRowCount;
CGFloat itemHeight = itemWidth / 3;
flowLayout.itemSize = CGSizeMake(itemWidth, itemHeight);