ios - UIView.convert 在计算 UIWindow 中的 UITableViewCell subview 时给出错误的结果

标签 ios iphone swift uitableview uiview

虽然我尝试在 UIWindow 内的 UITableViewCell 中转换 subview 的坐标,但它给出的结果没有添加 UINavigationBar 的高度,就好像它位于屏幕顶部,但它位于 UINavigationController 内部。也许我做错了什么。

open var targetView: UIView? {
    didSet {
        if let tView = targetView {
            targetFrame = self.convert(self.frame, to: tView)
        }
    }
}

self是UITableViewCell里面的子view,targetView是UIApplication.shared.keyWindow

最佳答案

我创建了一个最简单的应用程序来测试它,它运行良好。您可能错过了对 superview 的调用,尽管导航栏高度的确切差异看起来很奇怪。无论如何尝试:

targetFrame = self.superView!.convert(self.frame, to: tView) // TODO: remove the force-unwrap

我使用的是导航栏中带有委托(delegate)方法的表格 View :

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let view = tableView.cellForRow(at: indexPath)
    //  let view = tableView.cellForRow(at: indexPath)?.textLabel // Also works
        let parent = UIApplication.shared.keyWindow
        let converted = view?.superview?.convert(view!.frame, to: parent)
        print("\(converted)")
    }

打印的值确实包括导航栏。

关于ios - UIView.convert 在计算 UIWindow 中的 UITableViewCell subview 时给出错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41280331/

相关文章:

iphone - 用于分页的 RestKit 响应描述符

ios - 从 UITableCellView 和 UITableView 传输数据

ios - 在 swift 中分享单个 gif

ios - 如何抑制 "Availability does not match previous declaration"警告

ios - 更改所有屏幕的 ViewController 后退按钮图像

ios - 应用程序加载器警告 : The app references non-public selectors in Payload/<Appname>. app/<应用程序名称>:id

iphone - Xcode 4.5.1 iOS5/5.1 IBAction 不工作。在 iOS6 中很好

iphone - 在 iOS 应用程序中调用 "secret"URL 有多安全?

arrays - 追加包含其他类数组的类数组

ios - 是否可以使用 MKLocalSearch 获取 "restaurant"之类的位置类型