UICollectionView 自动调整高度 [英] UICollectionView autosize height

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

问题描述

如何正确调整 UICollectionView 的大小以使其完全显示其内容?我尝试了很多东西,包括设置它的框架、调用 reloadData 和使布局无效:

How do I properly resize a UICollectionView so that it fully displays its contents? I have tried many things, including setting its frame, calling reloadData and invalidating the layout:

self.collectionView.contentSize = CGSizeMake(300, 2000);
self.collectionView.frame = CGRectMake(0, 0, 300, 2000);
[self.collectionView reloadData];
[self.collectionView.collectionViewLayout invalidateLayout];

但是这些都没有任何效果.按下按钮后,我仍然看到初始视图,如下所示:

but none of this has any effect. After pressing the button I still see the initial view, like this:

我有一个小型演示程序,其中有一个生成 100 个元素的数据源.在 Interface Builder 中,我最初将 UICollectionView 的大小设置为一个较小的值,以便并非所有元素都适合,然后我按下一个按钮,然后执行上面的代码.我希望 UICollectionView 现在可以显示所有元素,但事实并非如此.

I have a small demo program where I have a data source producing 100 elements. In Interface Builder I initially set the size of the UICollectionView to a small value so that not all elements fit, after that I press a button after which the code above is executed. I expect the UICollectionView to now show all elements, but it doesn't.

可以在 https://github 找到演示程序.com/mjdemilliano/TestUICollectionView.

The demo program can be found at https://github.com/mjdemilliano/TestUICollectionView.

我观察到帧更新在某些时候会丢失,因为如果我再次按下按钮,当前帧会恢复到旧值.在按钮事件处理程序中添加一些日志语句后,日志输出为:

I have observed that the frame update is lost at some point, because if I press the button again, the current frame is back to the old value. After adding some log statements in the button event handler, the log output is:

before: frame = {{0, 58}, {320, 331}}, contentSize = {320, 1190}
update button pressed
after: frame = {{0, 0}, {300, 2000}}, contentSize = {300, 2000}
before: frame = {{0, 58}, {320, 331}}, contentSize = {320, 1190}
update button pressed
after: frame = {{0, 0}, {300, 2000}}, contentSize = {300, 2000}

我不明白为什么不保留框架变化,是什么在改变它.

在某些时候,我会将硬编码的值替换为从流布局中获得的值,但我想排除这种情况并让我的示例尽可能简单.

At some point I will replace the hardcoded values by values obtained from the flow layout, but I wanted to rule that out and keep my example as simple as possible.

上下文:我最终想要做的是:我有一个带有各种控件(如标签和图像)的可滚动视图,以及一个带有动态内容的集合视图.我想滚动所有这些,而不仅仅是集合视图,因此我没有使用集合视图自己的滚动工具,它工作正常.

Context: What I want to do eventually is the following: I have a scrollable view with various controls like labels and images, and a collection view with dynamic content. I want to scroll all that, not just the collection view, therefore I am not using the collection view's own scrolling facilities, which work fine.

推荐答案

我最终解决了这个问题,修复了所有自动布局问题,使用约束修复了集合视图的高度.然后,每当我知道内容已更改时,我都会使用值 collectionView.contentSize.height 更新约束的值:

I solved this eventually by fixing all Auto Layout issues, fixing the height of the collection view using a constraint. Then, whenever I know the content has changed I update the value of the constraint using the value collectionView.contentSize.height:

self.verticalLayoutConstraint.constant = self.collectionView.contentSize.height;

然后集合视图的大小被适当地调整,它在整个滚动视图中表现得很好.我已经用我的更改更新了 GitHub 测试项目.

Then the collection view is resized properly and it behaves nicely within the overall scrollview. I have updated the GitHub test project with my changes.

对我来说,通过手动更新约束而不是告诉 iOS:使集合视图的框架高度尽可能大"对我来说并不合适,但这是我最好的到目前为止已经想出了.如果您有答案,请发布更好的答案.

这篇关于UICollectionView 自动调整高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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