swift - 自动化 UICollectionViewCell 的宽度大小 - Swift

标签 swift uicollectionview uicollectionviewcell

如何让 UICollectionViewCell 自动设置 width size base size string

最佳答案

你可以这样做来计算字符串的宽度

let padding = 30

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {
    let lbl = UILabel() 
    lbl.text = arr[indexPath.item] // arr = [String]()
    let si = lbl.sizeThatFits(CGSize(width:CGFloat.greatestFiniteMagnitude, height:40)) 
    print(si.width)
    return CGSize(width:si.width + padding , height:40)
}

class VCName:UIViewController , UICollectionViewDelegate  , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout { }

关于swift - 自动化 UICollectionViewCell 的宽度大小 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58165970/

相关文章:

Xcode 7 错误代码“无法转换值类型([AnyObject]

ios - 确定您的应用程序之前是否在 iOS 中安装过

swift - Xcode Swift 编辑器中的这个颜色功能是什么?

ios - 为什么 Collection View 中的选定单元格在 UICollection View Swift 3 中无法正确显示?

ios - fatal error : unexpectedly found nil while unwrapping an Optional values

swift - 更新约束时如何避免 UICollectionView 中的淡入淡出动画

ios - 无法在 collectionView 中调用 dequeueReusableCellWithReuseIdentifier(collectionView : collectionViewLayout: sizeForItemAtIndexPath)

swift - 自定义 UITextField,不带参数初始化

ios - UITableViewCell 中的 UICollectionView

ios - 从父 UICollectionViewCell 中的 UICollectionViewCell 的按钮继续