如何正确调整UICollectionView的大小,使其完全显示其内容?我try 了很多方法,包括设置框架、调用reloadData
和使布局无效:
self.collectionView.contentSize = CGSizeMake(300, 2000);
self.collectionView.frame = CGRectMake(0, 0, 300, 2000);
[self.collectionView reloadData];
[self.collectionView.collectionViewLayout invalidateLayout];
但这些都没有任何效果.按下按钮后,我仍能看到初始视图,如下所示:
我有一个小的演示程序,其中有一个生成100个元素的数据源.在Interface Builder中,我最初将UICollectionView的大小设置为一个较小的值,以便不是所有元素都适合,然后按下一个按钮,然后执行上面的代码.我希望UICollectionView现在能显示所有元素,但事实并非如此.
EDIT: 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}
I don't understand why the frame change is not kept, what is changing it.
在某个时刻,我将用从流布局中获得的值替换硬编码的值,但我想排除这种可能性,并尽可能简单地保留我的示例.
上下文:我最终想做的是:我有一个带有标签和图像等各种控件的可滚动视图,以及一个带有动态内容的集合视图.我想滚动所有这些内容,而不仅仅是collection视图,因此我没有使用collection视图自己的滚动功能,这些功能工作得很好.