热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何通知UICollectionView框架更改-HowtobenotifiedofUICollectionViewframechanges

Thequestion:HowcanIbenotifiedwhentheframeofmyUICollectionViewchangessothatImayrun

The question:
How can I be notified when the frame of my UICollectionView changes so that I may run some code when this occurs? However, I wonder if that is the best solution to the problem.

问题:当我的UICollectionView的帧发生变化时如何通知我,以便在发生这种情况时运行一些代码?但是,我想知道这是否是解决问题的最佳方案。

The problem:
I have created my own UICollectionViewLayout which needs to know the collection view's frame size before laying out the cells. The problem is, the collection view does not appear on screen (yet) when I set its collectionViewLayout property to my custom layout in viewDidLoad. So when the layout code is run, the value it obtains for the collectionView.frame.size is the width and height defined in Interface Builder, which changes when the collection view appears on screen due to Autolayout constraints. This results in the custom layout returning an incorrect size in the function collectionViewContentSize.

问题:我创建了自己的UICollectionViewLayout,它需要在布局单元格之前知道集合视图的框架大小。问题是,当我在viewDidLoad中将其collectionViewLayout属性设置为我的自定义布局时,集合视图不会出现在屏幕上。因此,当运行布局代码时,它为collectionView.frame.size获取的值是Interface Builder中定义的宽度和高度,当集合视图由于Autolayout约束而出现在屏幕上时会更改。这导致自定义布局在函数collectionViewContentSize中返回不正确的大小。

The current solution:
To work around the problem, I don't change the collectionViewLayout until viewDidAppear. This does result in the obtaining the correct size. However, this is not done until after the collection view has been displayed with the default Flow layout, so all of the cells have been rendered in a different layout and then I change the layout and cause the cells to be re-rendered. While this is barely noticeable because it occurs so quickly, it's obviously not a good solution.

当前的解决方案:要解决此问题,我不会更改collectionViewLayout直到viewDidAppear。这确实导致获得正确的大小。但是,直到使用默认Flow布局显示集合视图之后才会执行此操作,因此所有单元格都以不同的布局呈现,然后我更改布局并使单元格重新呈现。虽然这几乎不可察觉,因为它发生得如此之快,但显然不是一个好的解决方案。

The better questions:
Is it possible to be notified when the size of a UICollectionView frame changes? If so, perhaps I could set the layout when that occurs which should occur soon enough to prevent displaying the cells with the default Flow layout.

更好的问题:当UICollectionView框架的大小发生变化时,是否可以通知?如果是这样,也许我可以在发生时设置布局,这应该很快发生,以防止使用默认的Flow布局显示单元格。

If that's not possible, a seemingly poor but slightly better solution could be to not display any cells until after the new layout is set in viewDidAppear. This would prevent the Flow layout from rendering, but would show a blank UI for a brief moment.

如果那是不可能的,那么在viewDidAppear中设置新布局之前,看似不好但稍好一点的解决方案可能是不显示任何单元格。这将阻止Flow布局呈现,但会在短时间内显示空白UI。

Another option would be to pass in a CGSize to the layout class instead of just accessing the collectionView.frame.size, and in doing so I could still set the custom layout in viewDidLoad and prevent displaying cells in Flow layout. This would require some way of knowing what the collection view size will be before it is rendered on screen which may not be possible (or at least not accurate).

另一种选择是将CGSize传递给布局类而不是仅仅访问collectionView.frame.size,这样做我仍然可以在viewDidLoad中设置自定义布局并阻止在Flow布局中显示单元格。这将需要某种方式来知道在屏幕上呈现之前集合视图大小将是什么,这可能是不可能的(或者至少不准确)。

What would be the best approach to solving this problem?

解决这个问题的最佳方法是什么?

Note that I don't need to worry about future collection view size changes, as the layout will be invalided and re-rendered upon bounds change.

请注意,我不需要担心将来的集合视图大小更改,因为布局将在边界更改时被无效并重新呈现。

1 个解决方案

#1


1  

You could try to use viewDidLayoutSubviews instead as it's called before viewDidAppear: so there is a chance that you can get the right frame values at that point.

您可以尝试使用viewDidLayoutSubviews,因为它在viewDidAppear之前调用:因此您可以在该点获得正确的帧值。

Note from the documentation:

请注意文档:

However, this method being called does not indicate that the individual layouts of the view’s subviews have been adjusted.

但是,调用此方法并不表示已调整视图子视图的各个布局。


推荐阅读
author-avatar
飞隔热条二
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有