问题
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?
回答1:
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;
}
回答2:
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;
回答3:
This works for me when using popovers from UIBarButtonItems.
- (void)buttonClicked:(id)sender{
{
CGRect buttonRect = [[sender view] frame];
}
回答4:
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.
回答5:
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)
}
回答6:
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
回答7:
UIToolbar *toolbar;
for (UIView *v in [toolbar items])
{
if ([v isKindOfClass:[UIBarButtonItem class]])
{
UIBarButtonItem *b = (UIBarButtonItem*)v;
//do something with b..
}
}
来源:https://stackoverflow.com/questions/8231737/how-to-determine-position-of-uibarbuttonitem-in-uitoolbar