如何使用 FlowLayout 确定 UICollectionView 的高度 [英] How to determine height of UICollectionView with FlowLayout

查看:44
本文介绍了如何使用 FlowLayout 确定 UICollectionView 的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 UICollectionViewFlowLayoutUICollectionView,我想计算它的内容大小(返回 intrinsicContentSize 需要调整它的高度通过 AutoLayout).

I've got an UICollectionView with an UICollectionViewFlowLayout, and i want to calculate its content size (for return in intrinsicContentSize needed for adjusting its height via AutoLayout).

问题是:即使我的所有单元格都有固定且相等的高度,我也不知道在 UICollectionView 中有多少行"/行.我也无法通过数据源中的项目数来确定该计数,因为表示数据项的单元格的宽度不同,因此我在 UICollectionView.

The problems is: Even if I have a fixed and equal height for all cells, I don't know how many "rows"/lines I have in the UICollectionView. I also can't determine that count by the number of items in my data source, since the cells representing the data items vary in width, so does consequently the number of items I have in one line of the UICollectionView.

由于我在官方文档中找不到关于这个主题的任何提示,谷歌搜索也没有给我带来任何进一步的信息,任何帮助和想法将不胜感激.

Since I couldn't find any hints on this topic in the official documentation and googling didn't bring me any further, any help and ideas would be appreciated very much.

推荐答案

哇!出于某种原因,经过数小时的研究,我现在找到了一个非常简单的问题的答案:我完全在错误的地方搜索,挖掘了我可以在 UICollectionView 上找到的所有文档.

Whoa! For some reason, after hours of research, I now found a pretty easy answer to my question: I was completely searching in the wrong place, digging through all the documentation I could find on UICollectionView.

简单易行的解决方案在于底层布局:只需在您的 myCollectionView.collectionViewLayout 属性上调用 collectionViewContentSize 即可获得内容的高度和宽度为 CGSize.就这么简单.

The simple and easy solution lies in the underlying layout: Just call collectionViewContentSize on your myCollectionView.collectionViewLayout property and you get the height and width of the content as CGSize. It's as easy as that.

这篇关于如何使用 FlowLayout 确定 UICollectionView 的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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