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

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

问题描述

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

我对其进行了测试,它可以正常工作(如果情节提要中的UIView高度大小大于或小于其内容,那么在运行时它将自动调整大小以匹配内容).

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

我还能做什么?

我在UIControllerView中也有2个部分(2列). 即使它们的大小内容不同,一行中的两个单元格也应具有相同的大小(类似这样,当将RecyclerView与GridLayoutManager一起使用时,这是在Android中本机实现的,非常容易)

更新

它可以与UIView一起使用,因为我将其最高约束条件设置为安全Are'a top"

我不能使用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

解决方案

感谢 https://stackoverflow.com,我解决了我的问题/a/25896386/7767664 https://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
    }
}

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天全站免登陆