【问题标题】:Make UICollectionViewCell's height match its content (sum height of all sub views) with AutoLayout使用 AutoLayout 使 UICollectionViewCell 高度匹配其内容(所有子视图的总高度)
【发布时间】:2019-09-04 17:10:26
【问题描述】:

我找到了让 UIView 的高度匹配其内容的下一个答案https://stackoverflow.com/a/39527226/7767664

我对其进行了测试,它工作正常(如果故事板中的 UIView 高度大小大于或小于其内容,则在运行时它会自动调整大小以匹配内容)。

但是如果我使用UICollectionViewCell 而不是UIView 那么什么都不会改变,单元格的高度永远不会改变,它总是具有我们在故事板属性中的硬编码高度:

我还能做什么?

我在 UIControllerView 中有 2 个部分(2 列)。 一行中的两个单元格应该具有相同的大小,即使它们的大小内容不同(当使用 RecyclerView 和 GridLayoutManager 时,在 Android 中原生实现了这样的操作,非常简单)

更新

它适用于UIView,因为我将其顶部约束设置为 Safe Are'a 顶部

UICollectionViewCell 做不到

更新 2

看来我对这个答案https://stackoverflow.com/a/25896386/7767664有一些进展

但我需要newFrame.size.height = CGFloat(ceilf(Float(size.height)))而不是newFrame.size.width = CGFloat(ceilf(Float(size.width)))

当我们使用这个解决方案时,不要在单元格底部添加任何约束,否则它将不起作用

使用此解决方案,我不能真正使用任何边距,否则某些部分在单元格底部变得不可见

我猜这个问题可以解决https://stackoverflow.com/a/31279726/7767664

【问题讨论】:

    标签: ios uicollectionview autolayout uicollectionviewcell ios-autolayout


    【解决方案1】:

    您可以尝试在您的 collectionView Extension 或您的本机 collectionViewController 类中调用此函数:

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
        {
            //return something like the size. I write you an example how to use it.
            // You can easily change the value according to your stuff contents height.
    
            return CGSize(width: collectionView.frame.size.width/3, height: 100)
        }

    【讨论】:

      【解决方案2】:

      感谢https://stackoverflow.com/a/25896386/7767664https://stackoverflow.com/a/31279726/7767664,我解决了我的问题

      我决定将其中包含所有需要的子视图的UIView 放入UICollecitonViewCell

      我将UIView 尾随、前导、顶部设置为ICollecitonViewCell,但我没有将底部设置为单元格视图(您应该在 UIView 中使用最新的子视图并连接它们的底部,而不是与单元格视图)

      然后我将 UIView 的引用添加到我的自定义单元格类中,并将其高度用作单元格的高度:

      public class MyCustomItemCell: UICollectionViewCell {
      
          // other child views references ...
      
          @IBOutlet weak var wrapperView: UIView! // view which contains your other views
      
          //forces the system to do one layout pass
          var isHeightCalculated: Bool = false
      
          override public func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
              //Exhibit A - We need to cache our calculation to prevent a crash.
              if !isHeightCalculated {
                  setNeedsLayout()
                  layoutIfNeeded()
                  var newFrame = layoutAttributes.frame
                  newFrame.size.height = wrapperView.frame.height // use height of our UIView
                  layoutAttributes.frame = newFrame
                  isHeightCalculated = true
              }
              return layoutAttributes
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多