Ios 如何在Swift中获得关于UIWindow的UIBarbuttonite框架?

Ios 如何在Swift中获得关于UIWindow的UIBarbuttonite框架?,ios,swift,uinavigationbar,uibarbuttonitem,uiwindow,Ios,Swift,Uinavigationbar,Uibarbuttonitem,Uiwindow,我已经阅读了很多关于这个话题的文章。但我有不同的要求 我正在我的应用程序中进行一个应用程序演练页面,我想在一些UI对象上显示一些提示。我想在整个页面上方放置一个UIView来模糊它,然后显示高亮显示的图像和提示文本。因此,我需要这些对象(需要帮助)相对于UIWindow的框架,以便我可以将另一个高亮显示的图像正好放在它们上面。我在tableview的标题中找到了几个对象的框架,代码如下 //Hint for expand/collapse arrow guard let deviceHeader

我已经阅读了很多关于这个话题的文章。但我有不同的要求

我正在我的应用程序中进行一个应用程序演练页面,我想在一些UI对象上显示一些提示。我想在整个页面上方放置一个UIView来模糊它,然后显示高亮显示的图像和提示文本。因此,我需要这些对象(需要帮助)相对于UIWindow的框架,以便我可以将另一个高亮显示的图像正好放在它们上面。我在tableview的标题中找到了几个对象的框架,代码如下

//Hint for expand/collapse arrow
guard let deviceHeaderView: StatusSectionView = tblStatus.headerView(forSection: 0) as? StatusSectionView else {
    //Hide expand/collapse hint objects
}

if let imgExpandCollapseFrame = deviceHeaderView.imgExpandCollapse.superview?.convert(deviceHeaderView.imgExpandCollapse.frame, to: window) {
    print("Frame of imgExpandCollapse.. \(imgExpandCollapseFrame)")

    let xPosition = (imgExpandCollapseFrame.origin.x + (imgExpandCollapseFrame.size.width/2)) - appWalkthroughView.downArrowHintImgView.frame.size.width/2

    let yPosition = (imgExpandCollapseFrame.origin.y + (imgExpandCollapseFrame.size.height/2)) - appWalkthroughView.downArrowHintImgView.frame.size.height/2

    appWalkthroughView.downArrowHintImgView.frame = CGRect(x: xPosition,
                                                           y: yPosition,
                                                           width: appWalkthroughView.downArrowHintImgView.frame.size.width,
                                                           height: appWalkthroughView.downArrowHintImgView.frame.size.height)
}
类似地,我需要找到UIBarButtonItem的框架,并将其框架相对于UIWindow进行转换,并在其上方显示提示图像

为此,我找到了像这样的uibarbuttonite的框架

let leftBarButtonItem = self.navigationItem.leftBarButtonItem!

if let leftBarButtonItemView = leftBarButtonItem.value(forKey: "view") as? UIView {
    print(leftBarButtonItemView.frame)
}
这段代码只是给出了条形按钮项的边界,如下所示

(0.0, 0.0, 36.0, 44.0)

我无法将它的框架相对于UIWindow进行转换。我们如何实现这一点?

您可以要求视图为您进行转换:

let frameInWindow = leftBarButtonItemView.convert(leftBarButtonItemView.bounds, to: nil)

(如果目标视图为零,convert将使用寡妇坐标系)

听到这个消息我很难过。坐标转换工作的一个先决条件是视图(在本例中为
LeftBarbuttoniemview
和应用程序的关键窗口)必须是同一视图层次结构的一部分(即
LeftBarbuttoniemview
必须是视图层次结构中窗口的后代)。如果当前未显示UINavigationBar或视图控制器,则可能不会出现这种情况。
let leftBarButtonItem = self.navigationItem.leftBarButtonItem
        if let leftBarButtonItemView = leftBarButtonItem?.value(forKey: "view") as? UIView {
            //You should ask your window to convert your frame to his frames.
            if let rect = UIApplication.shared.keyWindow?.convert(leftBarButtonItemView.frame, from: nil) {

            }
        }