使用 AutoLayout 使 UICollectionViewCell 的高度匹配其内容(所有子视图的总高度) [英] Make UICollectionViewCell's height match its content (sum height of all sub views) with AutoLayout

查看:22
本文介绍了使用 AutoLayout 使 UICollectionViewCell 的高度匹配其内容(所有子视图的总高度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了使 UIView 的高度匹配其内容的下一个答案

我还能做什么?

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

更新

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

我不能用 UICollectionViewCell

更新 2

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

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

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

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

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

解决方案

感谢 https://stackoverflow.com/a/25896386/7767664https://stackoverflow.com/a/31279726/7767664p>

我决定在 UICollecitonViewCell

中放置一个 UIView,其中包含所有需要的子视图

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

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

公共类 MyCustomItemCell: UICollectionViewCell {//其他子视图引用 ...@IBOutlet 弱 var wrapperView:UIView!//包含其他视图的视图//强制系统进行一次布局传递var isHeightCalculated: Bool = false覆盖 public func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) ->UICollectionViewLayoutAttributes {//Exhibit A - 我们需要缓存我们的计算以防止崩溃.如果 !isHeightCalculated {setNeedsLayout()布局如果需要()var newFrame = layoutAttributes.framenewFrame.size.height = wrapperView.frame.height//使用我们 UIView 的高度layoutAttributes.frame = newFrameisHeightCalculated = true}返回布局属性}}

I found the next answer to make UIView's height match its content https://stackoverflow.com/a/39527226/7767664

I tested it, it works fine (if UIView height size in storyboard bigger or smaller than its content then during runtime it autoresize itself to match the content).

But if I use UICollectionViewCell instead of UIView then nothing changes, height of cell is never changed, it always has the hardcoded height we have in storyboard properties:

What else can I do?

Also I have 2 sections in UIControllerView (2 columns). Two cells in one row should have the same size even if their size content is different (something like this implemented in Android natively when using RecyclerView with GridLayoutManager, very easy)

Update

It works with UIView because I set its top constraint to Safe Are'a top

I can't do it with UICollectionViewCell

Update 2

It seems I have some progress with this answer https://stackoverflow.com/a/25896386/7767664

But instead of newFrame.size.width = CGFloat(ceilf(Float(size.width))) I need newFrame.size.height = CGFloat(ceilf(Float(size.height)))

and when we use this solution, don't add any constraints to cell's bottom otherwise it will not work

With this solution I can't really use any margins otherwise some part of becomes invisible at the bottom of cell

I guess it can be solved with this question https://stackoverflow.com/a/31279726/7767664

解决方案

I solved my issue thanks to https://stackoverflow.com/a/25896386/7767664 and https://stackoverflow.com/a/31279726/7767664

I decided to put one UIView with all needed child views inside it to UICollecitonViewCell

I set UIView trailing, leading, top to ICollecitonViewCell but I didn't set bottom to cell view (you should use the latest child view inside UIView and connect their bottoms, not with the cell view)

Then I added reference of UIView to my custom cell class and I use its height for cell's height:

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
    }
}

这篇关于使用 AutoLayout 使 UICollectionViewCell 的高度匹配其内容(所有子视图的总高度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆