【问题标题】:Getting the screen location of a CollectionView cell from a UITableviewCell从 UITableviewCell 获取 CollectionView 单元格的屏幕位置
【发布时间】:2019-02-28 09:59:33
【问题描述】:

我有一个ViewController 和一个UITableView 在这个 tableView 的每个单元格中,我都有一个UICollectionView

我想获取我的 UICollectionView 的单元格的屏幕位置。

我已按照this 教程构建视图:

【问题讨论】:

  • 您可以使用let attributes = collectionView.layoutAttributesForItem(at: indexPath) let rect = attributes?.frame 这将为您提供特定索引路径的框架
  • 谢谢,但这给了我 tableViewCell 内的框架。但我需要知道整个屏幕内的单元格框架
  • 试试这个let centreWithMainView = collectionView.convertPoint(cell.center, toView: collectionView.superview)
  • let frame = cell.convert(cell.frame, to: cell.superview?.superview)
  • 这与您的第一个响应几乎相同。我已经尝试过了,但它无法工作,因为 collectionView 没有 viewController 作为超级视图:/

标签: ios swift uitableview frame


【解决方案1】:

您可以通过以下方式找到cellpoint

let realCenter = collectionView.convert(cell.frame, to: collectionView.superview)

但是如果这会给你collectionView。如果你需要找到关于main view的中心,你需要遍历所有的superview

let realCenter = collectionView.convert(cell.frame, to: collectionView.superview?.superview?.superview?.superview)

loop通过浏览找到你要找的superview然后找到frame

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 1970-01-01
    相关资源
    最近更新 更多