我正在尝试将 View 转换为另一个 viewController 的 collectionViewCell 的 imageView。为此,我需要将 collectionViewCell 中的 imageView 的框架转换为 Window。
我正在尝试使用 viewWillAppear 中的以下内容来做到这一点:
let indexPath = IndexPath(item: 0, section: 0)
let cell = collectionView.cellForItem(at: indexPath) as! ImageCollectionViewCell
let boundingRect = cell.imageView.convert(cell.imageView.bounds, to: nil)
boundingRect 的大小是正确的,但是由于某种原因原点位于 0,0 而不是它应该在的位置。
我想知道问题是否在于 collectionView 尚未布局,因此来源错误。如果是这样的话,我怎么能强制它布局。我试过:
collectionView.setNeedsLayout()
collectionView.layoutIfNeeded()
但这也没有改变原点。
我如何强制 collectionView 的布局并在 viewDidAppear 之前获取特定单元格(或该单元格中的 imageView)的框架。我想避免在 viewWillLayoutSubviews 或 viewDidLayoutSubviews 中这样做,以防止它不断运行。
最佳答案
根据 https://developer.apple.com/documentation/uikit/uiview/1622498-convert我们需要:
- view (collectionView.superview),我们想要获取 subview 的地方 地点,
- subview 的框架(cell.imageView.frame)
- 和 subview 的父 View 或 superview.superview 等,任何包含 subview 的框架(单元格)的 View
我们得到
let boundingRect = collectionView.superview.convert(cell.imageView.frame, from: cell)
关于ios - 无法获取用于转换的 collectionViewCell 项目的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48924675/