ios - 更新 UICollectionView 大小的奇怪问题
问题描述
我有一个 UICollectionView,它的初始高度是 50。当我知道单元格的大小时,我想更新它。对于我所做的:
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
let categoriesCount = 16
let numberOfItemsInRow = 3
let numberOfRows = ceil(CGFloat(categoriesCount) / CGFloat(numberOfItemsInRow))
let newHeight = (numberOfRows * cell.frame.size.height)
collectionHeight.constant = newHeight
collectionView.collectionViewLayout.invalidateLayout()
}
但是尽管调用invalidateLayout
了 - collectionView 的高度保持不变 - 50。
但是当我切换屏幕时,当我第二次使用 UICollectionView 打开该屏幕时,它会显示正确的高度。
我做错了什么,如何更新 UICollectionView 的高度?
我什至尝试过collectionView.updateConstraints()
,collectionView.collectionViewLayout.invalidateLayout
但仍然没有运气
更新
我也试过
collectionView.layoutIfNeeded()
self.superview?.layoutIfNeeded()
但没有结果。
解决方案
推荐阅读
- python - 如何在python的循环中打开和关闭可执行文件
- r - shapefiles:在 R 中将点(MultiPoint)转换为点(PointZ)几何
- javascript - 如何在反应原生的一个redux中添加多个计数器
- input - 为什么 Nyr OpenVPN 安装脚本需要“read -N 999999 -t 0.001”?
- javascript - 为什么当用户提交输入时我的 html 选择选项不更新?
- javascript - Expo OTA 更新是否适用于新库?
- javascript - 从 for 循环内函数的输出创建一个数组
- npm - 纱线因节点 gyp 错误而失败
- c# - UnityEngine.UI.Text 没有在异步方法中更新
- javascript - TypeError:null 不是对象(评估'_this2.emailRef.current.focus - 如何在打开屏幕时自动聚焦输入字段