How to make dynamic width of UICollectionViewCell

I want to set the width UICollectionViewCelldynamically. I found the code in swift and I need 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)
    }
+4
source share
5 answers

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);
    }
}
+4
source

This is the simplest solution.

Your UIViewController should implement a UICollectionViewDelegateFlowLayout. Then the following function should 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)

}

In addition, you can adjust the distance between cells in the storyboard. Click CollectionView, and then view the size inspector.

+4
source

, :

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

CollectionView

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.view.frame.size.width*20/100, 192.f);
}
+1
source

Swift 2.2

override func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    var flowLayout = (collectionView.collectionViewLayout as! UICollectionViewFlowLayout)
    if indexPath.item % 3 == 0 {
        var cellWidth: Float = (CGRectGetWidth(collectionView.frame) - (flowLayout.sectionInset.left + flowLayout.sectionInset.right))
        return CGSizeMake(cellWidth, cellWidth / 2)
    }
    else {
        var cellWidth: Float = (CGRectGetWidth(collectionView.frame) - (flowLayout.sectionInset.left + flowLayout.sectionInset.right) - flowLayout.minimumInteritemSpacing) / 2
        return CGSizeMake(cellWidth, cellWidth)
    }
}

Swift 3.0

override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    var flowLayout: UICollectionViewFlowLayout? = (collectionView.collectionViewLayout as? UICollectionViewFlowLayout)
    if indexPath.item % 3 == 0 {
        var cellWidth: Float? = (collectionView.frame.width - (flowLayout?.sectionInset?.left + flowLayout?.sectionInset?.right))
        return CGSize(width: CGFloat(cellWidth), height: CGFloat(cellWidth / 2))
    }
    else {
        var cellWidth: Float? = (collectionView.frame.width - (flowLayout?.sectionInset?.left + flowLayout?.sectionInset?.right) - flowLayout?.minimumInteritemSpacing) / 2
        return CGSize(width: CGFloat(cellWidth), height: CGFloat(cellWidth))
    }
}
0
source

if there is text in the collection view cell, you just need to refer to some label in the scene with no width limit, and then each time in size for the element just assign it your own text and call

Goal 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 a _outletoflbllabel for assigning text to it to get the width of the most suitable font and other attributes that you assign from the interface constructor!

Result of checking:

Auto fit width of text

0
source

Source: https://habr.com/ru/post/1652974/


All Articles