带有两个动态标签的UICollectionViewCell动态高度&自动布局 [英] UICollectionViewCell dynamic height w/two dynamic labels & auto layout

查看:108
本文介绍了带有两个动态标签的UICollectionViewCell动态高度&自动布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UICollectionViewCell子类,该子类使用原型单元格和约束条件进行设置(每个视图都垂直和水平连接).

I have a UICollectionViewCell subclass that's setup with a prototype cell and constraints (every view is connected both vertically and horizontally).

我有两个标签,它们的大小可以不同,它们可以是一行或两行.为此,我在每个标签上设置了两个高度限制,一个大于或等于(取决于标签为16或20)而小于或等于(取决于标签为32或40).

I have two labels that can vary in size, they can be either one or two lines. To that end I have set two height constraints on each label, one with a greater than or equal (16 or 20 depending on the label) and a less than or equal (32 or 40 depending on the label).

标签上的行数设置为0.(尽管我尝试了各种设置).

The number of lines on the labels are set to 0. (Though I have tried a variety of settings).

由于使用了自动布局和约束来设置视图,因此我在单元格的awakeFromNib()中的内容视图上指定了宽度

Since I've used auto layout and constraints to setup the view I've specified a width on the content view in the cell's awakeFromNib()

override func awakeFromNib() {
        NSLayoutConstraint.activate([contentView.widthAnchor.constraint(equalToConstant: 341)])
        super.awakeFromNib()
        configureBorder()
}

在collectionView的布局上,我为estimateItemSize&指定了automaticDimension常量. itemSize

On the collectionView's layout I've specified the automaticDimension constant for estimateItemSize & itemSize

if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
    flowLayout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
    flowLayout.itemSize = UICollectionViewFlowLayoutAutomaticSize
}

出于使用自动布局的想法,我在集合视图上实现了委托.

With the idea of using auto layout I have not implemented a delegate on the collection view.

我遇到了非常不一致的布局,在某些情况下该单元格会按照我的意愿出现,在另一些情况下,标签被截断了,在另一些情况下,标签却非常狭窄,并在三行上显示了文本.

I am experiencing very inconsistent layout, some instances the cell's will appear as I'd like them to, in others the labels are truncated and in others the labels are extremely narrow and display text over three lines.

我尝试了很多事情,包括打电话 .sizeToFit() 在每个标签上设置了text属性后,以及调用 .setNeedsDisplay() 单元格从cellForItemAt

I have tried many things, including calling .sizeToFit() on each label after it's text property is set, as well as calling .setNeedsDisplay() before the cell returns from cellForItemAt

我想知道采用情节提要约束的正确方法是什么?我曾经在SO上针对类似问题解答过一些流行的答案,尽管并没有取得任何真正的成功.

I'm wondering what is the right way to do this with storyboard constraints. I've worked with some of the popular answers to similar questions here on SO, though none to any real success.

推荐答案

flowLayout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
flowLayout.itemSize = UICollectionViewFlowLayoutAutomaticSize

当然,我熟悉Apple的一再声明,即UICollectionViewFlowLayout中的UICollectionViewCell可以根据内部约束自动调整大小.但是我从来没有见过苹果公司演示过此功能的有效代码,而且我<从未> 从未成功地确认这些说法是正确的.我不相信真的有一个自定义大小的单元.多年以来,试图使单元自动调整大小会导致崩溃.在iOS 10中,崩溃几乎停止了,但是现在流布局没有正确布置单元格(听起来像您正在看到的内容).

Of course I am familiar with Apple's repeated claims that a UICollectionViewCell in a UICollectionViewFlowLayout can be self-sizing based on internal constraints. But I have never seen working code from Apple that demonstrated this feature, and I have never once myself succeeded in confirming those claims to be true. I do not believe there is really such a thing as a self-sizing cell. For years, trying to get a cell to be self-sizing resulted in crashes. In iOS 10, the crashing mostly stopped, but now the flow layout was not laying out the cells correctly (which sounds like what you're seeing).

自然地,我年复一年都提交了错误报告.

Naturally, I have filed bugs on all this, year after year.

同时,我要做的是为集合视图提供委托并实现collectionView(_:layout:sizeForItemAt:),我建议您也应该这样做.

Meanwhile, what I do is to give the collection view a delegate and implement collectionView(_:layout:sizeForItemAt:), and I suggest that you should do the same.

编辑是iOS 13的新功能,您可以使用可组合布局并完全放弃UICollectionViewFlowLayout.自调整大小的单元格在可组合的布局中可以很好地工作.

EDIT New in iOS 13 you can use a composable layout and abandon UICollectionViewFlowLayout altogether. Self-sizing cells work fine in a composable layout.

这篇关于带有两个动态标签的UICollectionViewCell动态高度&amp;自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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