Re: any way to know where a UIBarButtonItem has been drawn?
Re: any way to know where a UIBarButtonItem has been drawn?
- Subject: Re: any way to know where a UIBarButtonItem has been drawn?
- From: Fritz Anderson <email@hidden>
- Date: Mon, 12 Sep 2011 13:55:33 -0500
On 12 Sep 2011, at 11:53 AM, Roland King wrote:
> However .. I can't figure out what the frame for the UIBarButtonItem is .. it's on a bar with items which change, some flexible space etc, so it's not a fixed offset from either end of the the thing, and I can't find an API which tells me what the frame is for a given item. Is there one I've missed?
I can't find a frame either, but the bar will give you an array of items, and assuming all the items are UIBarButtonItem, you can iterate over the array to get their widths. From that you can get a good enough rectangle for visual purposes. Here's what I'd start with (composed in Mail, untested):
- (CGRect) barButtonFrame: (UIBarButtonItem *) aButton
{
// Assumes you are holding a UIToolbar in self.toolbar.
// Assumes everything in the toolbar responds to -width
// (i.e., UIBarButtonItem, which includes spacers).
// The toolbar observes an inset for the first item.
// Figure out the inset empirically, and adjust BAR_MARGIN accordingly:
#define BAR_MARGIN 4.0
BOOL found = NO;
CGRect buttonFrame = self.toolbar.bounds;
CGFloat totalWidth = 0.0;
for (UIBarButtonItem * item in self.toolbar.items) {
if (item == aButton) {
// Found it.
buttonFrame.origin.x = totalWidth + BAR_MARGIN;
buttonFrame.size.width = item.width;
found = YES;
break;
}
else
// Still looking.
totalWidth += item.width;
}
return found? buttonFrame: CGRectZero;
// The rect is in the coordinates of self.toolbar.
// Convert to self.view coordinates (or whatever) for the animation.
// Check for CGRectZero (button not found). There's an argument to
// be made that this method should raise an exception if ! found.
}
— F
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden