Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make dynamic width of the UICollectionViewCell

I want to set the width of UICollectionViewCell dynamically. I found some code in swift and I want the code in Objective-C.

let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
    if indexPath.item % 3 == 0 {
        let cellWidth = (CGRectGetWidth(collectionView.frame) - (flowLayout.sectionInset.left + flowLayout.sectionInset.right))
        return CGSize(width: cellWidth, height: cellWidth / 2)
    } else {
        let cellWidth = (CGRectGetWidth(collectionView.frame) - (flowLayout.sectionInset.left + flowLayout.sectionInset.right) - flowLayout.minimumInteritemSpacing) / 2
        return CGSize(width: cellWidth, height: cellWidth)
    }
like image 646
sanjana Mishra Avatar asked Aug 30 '16 09:08

sanjana Mishra


4 Answers

This is the simplest solution.

Your UIViewController should implement UICollectionViewDelegateFlowLayout. Then the function below needs to be implemented on your UIviewController.

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let width = self.collectionView.frame.size.width / 2;


    return CGSize(width: width, height: 230)

}

Furthermore, you can adjust each cell's spacing value on your storyboard. Click collectionView then see the size inspector.

like image 75
Sunjoong Kevin Kim Avatar answered Nov 09 '22 17:11

Sunjoong Kevin Kim


You can try this...

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout*) collectionView.collectionViewLayout;

    if (indexPath.item % 3 == 0) {
        float cellWidth = (CGRectGetWidth(collectionView.frame) - (flowLayout.sectionInset.left + flowLayout.sectionInset.right));
        return CGSizeMake(cellWidth, cellWidth / 2);
    } else {
        float cellWidth = (CGRectGetWidth(collectionView.frame) - (flowLayout.sectionInset.left + flowLayout.sectionInset.right) - flowLayout.minimumInteritemSpacing) / 2;
        return CGSizeMake(cellWidth, cellWidth);
    }
}
like image 44
Sailendra Avatar answered Nov 09 '22 18:11

Sailendra


set cell's width to a percentage of the container view like:

cell.frame = CGRectMake(x, y, self.view.frame.size.width * 20 / 100, height);

or you can use CollectionView Delegates

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.view.frame.size.width*20/100, 192.f);
}
like image 1
roozbeh mo Avatar answered Nov 09 '22 18:11

roozbeh mo


in case collection view cell has text, you just need to reference some label in the scene without width constraint for it, then every time in size for item, just assign to it your text, and call

Objective C:

NSString *text = [arrayOfItems objectAtIndex:indexPath.item];
_outletoflbl.text = text;
return CGSizeMake(_outletoflbl.intrinsicContentSize.width + 60, 40);

Swift:

let text = arrayOfItems[indexPath.item]
_outletoflbl.text = text
return CGRect(width:_outletoflbl.intrinsicContentSize.width + 60, height: 40)

The key is _outletoflbl, the label to assign it text to get width of it best fitting font and other attributes you assign from interface builder!

Check result:

Auto fit width of text

like image 1
IsPha Avatar answered Nov 09 '22 19:11

IsPha