【问题标题】:Swift : UICollectionView Cell can not update frame when implement sizeForItemAtIndexPathSwift:实现 sizeForItemAtIndexPath 时 UICollectionView Cell 无法更新框架
【发布时间】:2017-11-14 00:55:21
【问题描述】:

当我实现以下代码时:

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

    let cellW = (ScreenWidth - 10 - 10 - 30) / 4

    return CGSize(width: cellW, height: cellW * 1.5 )

}

通过使用 sizeForItemAt 方法,我可以绘制我想要的框架,但是我的自定义 collectionview 单元格无法更新,我错过了哪里,请帮助我!!

my custom cell

debug view

【问题讨论】:

  • 将图像的约束添加到前导/尾随/顶部到 superView?并为底视图添加缺少的约束?
  • 我确实为我的 imageview 和 label 添加了约束,这似乎是 collectionview 单元格的问题,也许我在 awakefromNib 中遗漏了一些东西? imgur.com/a/6N9qC
  • imgur.com/a/XOVTn 这是我的故事板集合视图单元格框架
  • 你写的是你在类声明中实现了 UICollectionViewDelegateFlowLayout 吗?
  • 是的,我确实实现了 UICollectionViewDelegateFlowLayout 委托,以便我可以绘制该区域(请参阅调试视图:imgur.com/a/TyjOr

标签: ios swift3 uicollectionview uicollectionviewcell


【解决方案1】:

我通过这个参考修复它 UICollectionView cell subviews do not resize

谢谢大家,首先我的 Cell.imageview 仍然有问题,不能按我想要的形状做圆形,我为 imageview 创建了一个类,如下:

class CellImageView: UIImageView {
override func layoutSubviews() {
    super.layoutSubviews()
    let radius: CGFloat = self.bounds.size.width / 2.0
    self.layer.cornerRadius = radius
    self.clipsToBounds = true

}

任何东西都可以完美运行

【讨论】:

    【解决方案2】:

    对于 iOS 13 中的我,我先将 estimatedItemSize 固定为 .zero 并设置 itemSize 之类的;

    if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
     layout.estimatedItemSize = .zero
     layout.itemSize = CGSize(width: collectionView.frame.size.width, height: 80)
    }
    

    【讨论】:

      猜你喜欢
      • 2015-04-01
      • 1970-01-01
      • 2021-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多