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

查看:1017
本文介绍了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.

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

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;

然后正确调整集合视图的大小,它在整个scrollview中表现良好。我用我的更改更新了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天全站免登陆