What's the easiest way to determine the x,y location of a UIBarButtonItem in a UIToolbar?
The only answer I found is in any way to know where uibarbuttonitem has been drawn.
All proposed answers seem too complicated. There ought to be a simpler way to get the position of the damn UIBarButtonItem isn't there?
For iOS 11 (and maybe above) the call:
if let view = barButtonItemView.value(forKey: "view") as? UIView {
let viewFrame = view.frame
}
will return a zero point origin for the view. To counter this, ask for the window coordinates of the view, by using:
let actualPointOnWindow = view.convert(view.frame.origin, to: nil)
If your toolbar has any translations subtract them from the calculated point to find its position on the toolbar.
Complete Code:
if let view = barButtonItemView.value(forKey: "view") as? UIView {
let viewFrame = view.frame
let actualPointOnWindow = view.convert(view.frame.origin, to: nil)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With