ios - 如何在 Swift 中获取 UIBarButtonItem 相对于 UIWindow 的框架?
问题描述
我已经阅读了很多关于这个主题的 SO 帖子。但我有不同的要求。
我正在处理我的应用程序中的应用程序演练页面,我想在某些 UI 对象上显示一些提示。我想在整个页面上方放置一个 UIView 以使其模糊,然后显示突出显示的图像和提示文本。出于这个原因,我需要这些对象(需要帮助)相对于 UIWindow 的框架,以便我可以将另一个突出显示的图像正好放在它们上方。我使用以下代码在表格视图的标题中找到了几个对象的框架。
//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 的框架转换并在其上方显示提示图像。
为此,我找到了这样的 UIBarButtonItem 框架,
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 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) {
}
}
推荐阅读
- erlang - 如何在 Erlang escript 中设置代理?
- python - 如何处理以下异常 - 'PngStream' 对象没有属性 'chunk_eXIf'
- mysql - 计算 @balance 返回 null
- github - GitHub gh-pages 站点为特定文件返回 404
- sftp - 如何使用 SFTP 输入/输出 Google Dataflow
- r - 在函数中传递变量名
- seo - robots.txt 中的错误即使已修复也会不断堆积
- atg - 更新存储库项目时出错
- postgresql - 添加限制子句时插入选择获得更好的计划
- html - 如何以垂直格式将查询中的数据显示到mvc中的视图?