热门标签 | 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.

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


推荐阅读
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • ImmutableX Poised to Pioneer Web3 Gaming Revolution
    ImmutableX is set to spearhead the evolution of Web3 gaming, with its innovative technologies and strategic partnerships driving significant advancements in the industry. ... [详细]
  • 本文探讨了如何在不重新加载URL的情况下,触发WebView的PictureListener.onNewPicture()方法,以实现页面的重新绘制或渲染。 ... [详细]
  • 本文详细介绍了如何在 Windows 环境下使用 node-gyp 工具进行 Node.js 本地扩展的编译和配置,涵盖从环境搭建到代码实现的全过程。 ... [详细]
  • 在 Flutter 开发过程中,开发者经常会遇到 Widget 构造函数中的可选参数 Key。对于初学者来说,理解 Key 的作用和使用场景可能是一个挑战。本文将详细探讨 Key 的概念及其应用场景,并通过实例帮助你更好地掌握这一重要工具。 ... [详细]
  • Qt QTableView 内嵌控件的实现方法
    本文详细介绍了在 Qt QTableView 中嵌入控件的多种方法,包括使用 QItemDelegate、setIndexWidget 和 setIndexWidget 结合布局管理器。每种方法都有其适用场景和优缺点。 ... [详细]
  • Python 异步编程:深入理解 asyncio 库(上)
    本文介绍了 Python 3.4 版本引入的标准库 asyncio,该库为异步 IO 提供了强大的支持。我们将探讨为什么需要 asyncio,以及它如何简化并发编程的复杂性,并详细介绍其核心概念和使用方法。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 本文详细探讨了Java中的24种设计模式及其应用,并介绍了七大面向对象设计原则。通过创建型、结构型和行为型模式的分类,帮助开发者更好地理解和应用这些模式,提升代码质量和可维护性。 ... [详细]
  • ListView简单使用
    先上效果:主要实现了Listview的绑定和点击事件。项目资源结构如下:先创建一个动物类,用来装载数据:Animal类如下:packagecom.example.simplelis ... [详细]
  • 本文详细介绍了如何在 Android 中使用值动画(ValueAnimator)来动态调整 ImageView 的高度,并探讨了相关的关键属性和方法,包括图片填充后的高度、原始图片高度、动画变化因子以及布局重置等。 ... [详细]
  • 本文详细介绍了 Android 开发中 layout_gravity 属性的使用方法及其在不同布局下的效果,旨在帮助开发者更好地理解和利用这一属性来精确控制视图的布局。 ... [详细]
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社区 版权所有