Hisham
Hisham

Reputation: 1325

How to determine position of UIBarButtonItem in UIToolbar?

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?

Upvotes: 13

Views: 16777

Answers (7)

Don Miguel
Don Miguel

Reputation: 912

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)
}

Upvotes: 5

Matjan
Matjan

Reputation: 3607

A more 'swifty' SWIFT way:

func userAction(sender: AnyObject) {
    if let originView = sender.valueForKey("view") {
        let frame = originView.frame  //it's a UIBarButtonItem
    } else {
        let frame = sender.frame //it's a regular UIButton
    }
}

UIBarButtonItem is NOT a subclass of UIView even though it's essentially a button. Go figure.

Upvotes: 3

user3687284
user3687284

Reputation: 2236

I used johosher's approach to get the position of a UIBarButtonItem using Swift.

Since I needed the position as sourceRect for a popoverPresentationController I had to convert it to self.view. I am not sure, whether this is a good solution but it works very well and the popover shows off right of the UIBarButtonItem.

let senderRect = sender.view!!.convertRect(sender.view!!.bounds, toView: self.view)

// additional code for the popover controller
alertController.modalPresentationStyle = UIModalPresentationStyle.Popover
alertController.popoverPresentationController?.sourceRect = senderRect
alertController.popoverPresentationController?.sourceView = self.view

Upvotes: 2

Robert Hill
Robert Hill

Reputation: 353

This works for me when using popovers from UIBarButtonItems.

- (void)buttonClicked:(id)sender{
{
  CGRect buttonRect = [[sender view] frame];
}

Upvotes: 6

johosher
johosher

Reputation: 329

I used this which seems to be the most elegant way

- (CGRect)frameForBarButtonItem:(UIBarButtonItem *)buttonItem
{
    UIView *view = [buttonItem valueForKey:@"view"];
    return  view ? view.frame : CGRectZero;
}

Upvotes: 19

morex87
morex87

Reputation: 9

UIToolbar *toolbar;

for (UIView *v in [toolbar items]) 
{
    if ([v isKindOfClass:[UIBarButtonItem class]])
    {
        UIBarButtonItem *b = (UIBarButtonItem*)v;
        //do something with b..
    }
}

Upvotes: 0

James Hu
James Hu

Reputation: 842

Unfortunately, there is no easy way of determining the position of a UIBarButtonItem. A UIBarButtonItem is essentially a NSObject that does just two things: describe the look and feel of a toolbar button, and forward events to the designated target/action selector.

Now, given that all buttons are subviews of UIToolbar, and all button events are routed through their respective UIBarButtonItems, it's quite trivial to loop through all subviews of your UIToolbar and when you find a button whose target is that of your UIBarButtonItem, just get the frame of that button. Some code:

UIToolbar *toolbar = <your toolbar>;
UIBarButtonItem *barButtonItem = <your item>;
UIButton *button = nil;
for (UIView *subview in toolbar.subviews) {
    if ([subview isKindOfClass:[UIButton class]]) {
        for (id target in [(UIButton *)subview allTargets]) {
            if (target == barButtonItem) {
                button = (UIButton *)subview;
                break;
            }
        }
        if (button != nil) break;
    }
}
CGRect frame = button.frame;

Upvotes: 13

Related Questions