how to make dynamic width of the UICollectionViewCell

前端 未结 5 1434
粉色の甜心
粉色の甜心 2021-01-03 05:59

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 = col         


        
5条回答
  •  暖寄归人
    2021-01-03 06:37

    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:

提交回复
热议问题