Reputation: 740
I have gone through a lot of SO posts on this topic. But I have a different requirement.
Am working on an App Walkthrough page in my app for which i would like to display some hints over some of the UI objects. I would like to place a UIView above the entire page to blur it out and then display highlighted image and a hint text. For that reason, I need the frame of those objects(for which help is required) with respect to the UIWindow so that i can place another highlighted image exactly above them. I have found the frame of a couple of objects inside the header of a tableview with the following code.
//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)
}
Similarly, I need to find the frame of the UIBarButtonItem and convert its frame with respect to the UIWindow and display hint image above it.
For that, I have found the frame of the UIBarButtonItem like this,
let leftBarButtonItem = self.navigationItem.leftBarButtonItem!
if let leftBarButtonItemView = leftBarButtonItem.value(forKey: "view") as? UIView {
print(leftBarButtonItemView.frame)
}
This code just gives the bounds of the bar button item like this.
(0.0, 0.0, 36.0, 44.0)
And i couldn't convert its frame with respect to UIWindow. How can we achieve this?
Upvotes: 1
Views: 2228
Reputation: 3677
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) {
}
}
Upvotes: 1
Reputation: 1984
You could ask the view to do the conversion for you:
let frameInWindow = leftBarButtonItemView.convert(leftBarButtonItemView.bounds, to: nil)
(convert will use the widow's coordinate system is the target view is nil)
Upvotes: 0