How can I obtain the origin of UIBarButtonItem
added to UINavigationBar
, I searched for all question but always find the size instead of frame.
Asked
Active
Viewed 476 times
0
-
Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Apr 13 '18 at 09:31
-
1Why are you trying to get the origin? – Apr 13 '18 at 09:35
-
I need to pass that frame for presenting a view from that button. – Preeti Apr 13 '18 at 09:48
-
Why you need to pass the frame? Simply attach an action to that button (eg. https://stackoverflow.com/questions/2333638/how-to-set-target-and-action-for-uibarbuttonitem-at-runtime) – Apr 13 '18 at 13:18
-
@IanBell Thats true but the requirement is too display a userguide from that button without user interaction, and I am using third party framework for displaying userguide where I need to pass view frame.I am even getting the y coordinate by view conversion but x always comes zero. – Preeti Apr 13 '18 at 16:50
1 Answers
-1
You can get the frame of the bar button item in the coordinate system of self.view, and then get the origin from that:
let barButtonFrame: CGRect? = {
if let barButtonView = self.barButton.value(forKey: "view") as? UIView {
return self.view.convert(barButtonView.bounds, from: barButtonView)
}
return nil
}()
barButtonFrame?.origin // CGPoint?
barButtonFrame?.size // CGSize?

David Steppenbeck
- 984
- 7
- 13
-
This does not work for me on iOS 14. It gives some numbers very close to zero that are obviously wrong. – förschter Jan 11 '21 at 11:59