I have a UIBarButtonItem
with UIButton
as custom view.
The UIButton
has a addTarget:action:
on it. In the action
Not sure that you need the UIButton. If what you're doing is dispatching an action, or even customizing the button with an image, the UIBarButtonItem
should suffice.
If what you're after is to grab the frame from the UIButton
in order to achieve the presentation, I think you might be better served to simply estimate the position of the UIBarButtonItem
. This shouldn't be too difficult, especially if it's one or the other of your UINavigationItem
's leftBarButtonItem
or rightBarButtonItem
.
I'm generally for the KISS (Keep It Simple, Stupid!) rule of thumb. Even Apple does this... when you launch an app from Springboard, the app always expands from the center of the screen, not from the app's icon.
Just a suggestion.
EDIT
OK, I just read the UIPopoverController reference (I've never used one). I think what you want is presentPopoverFromBarButtonItem:permittedArrowDirections:animated:
and to pass in your BBI as the first parameter. The reason that method exists is to solve your problem - BBI's have no frame because they're not subclasses NSView. Apple knows that you want to do this kind of thing, and provides this method. Also, I think if you use this method, that your autorotation will work as well. I may be wrong about this, give it a shot.
As far as your customized layout is concerned, I think if you replicate it in a UIView and make the BBI custom with that, you will do better. This is of course up to you.
Either way, you get a reference to the BBI by either connecting it as an IBOutlet with your NIB, or by saving a reference to it when you create it in code. Then just pass that reference to the popover method I described above. I think this might work for you.
MOAR
The BBI is just a member of your class - an iVar with a strong reference property on it, perhaps linked up as an IBOutlet to your NIB. Then you can access it from whatever method you want in the class.
Example: (not sure that I have the popover controller memory management right)
@interface MyViewController : UIViewController {
UIBarButtonItem *item;
}
@property (nonatomic, retain) UIBarButtonItem *item;
@end
@implementation MyViewController
@synthesize item;
-(void)viewDidLoad {
// assuming item isn't in your NIB
item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlus target:self action:@selector(doit)];
self.navigationItem.rightBarButtonItem = item;
}
-(void)doit {
UIPopoverController *popover = [[[UIPopoverController alloc] initWithContentViewController:yourViewController] autorelease];
[popover presentPopoverFromBarButtonItem:self.item permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
// or in the line above, replace self.item with self.navigationItem.rightBarButtonItem
}
@end