I am optimizing an app for iPad at the moment and would like to use a UIBarButtonItem with an image to present a popover. The code I currently have works great if I am using a button with a title but I would prefer to use an image of my own or one of those provided with a UIBarButtonSystemItem. If I do not set the button’s title at all, it simply will not show up. If I set a title AND an image, I get the title on real hardware but the image shows up on the simulator. Using a UIBarButtonSystemItem, I get the button I want but it appears in landscape mode as well (which it shouldn’t do because I am using a split view for landscape).
Using any combination of setImage, setTitle, or initializing the button as a system preset has not worked. I have also tried setting the button with a system preset to nil when a rotation to landscape was detected but it remained on the screen. I am unsure of how to proceed from here but I would really like to avoid using a string for this button, plus I am very curious to know why this is happening.
Here is the code:
#pragma mark Split view handling
-(void)splitViewController:(UISplitViewController *)svc
willHideViewController:(UIViewController *)aViewController
withBarButtonItem:(UIBarButtonItem *)barButtonItem
forPopoverController:(UIPopoverController *)pc
{
//If this bar button item doesn't have a title, it won't appear at all.
[barButtonItem setTitle:@"-"];
//barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:[barButtonItem target] action:[barButtonItem action]];
[barButtonItem setImage:[UIImage imageNamed:@"listing.png"]];
//Take this bar button item and put it on the left side of our nav item
[[self navigationItem] setLeftBarButtonItem:barButtonItem];
self.popoverController = pc;
}
-(void)splitViewController:(UISplitViewController *)svc
willShowViewController:(UIViewController *)aViewController
invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
//Remove the bar button item from our navigation item.
//We'll double check that it's the correct button, even though we know it is.
if(barButtonItem == [[self navigationItem] leftBarButtonItem]){
[[self navigationItem] setLeftBarButtonItem:nil];
}
self.popoverController = nil;
}
UPDATE:
I added this to my viewDidLoad:
customButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"listing.PNG"]]];
customButtonItem.style = UIBarButtonItemStyleBordered;
And updated willHideViewController to this:
-(void)splitViewController:(UISplitViewController *)svc
willHideViewController:(UIViewController *)aViewController
withBarButtonItem:(UIBarButtonItem *)barButtonItem
forPopoverController:(UIPopoverController *)pc
{
//If this bar button item doesn't have a title, it won't appear at all
customButtonItem.target = barButtonItem.target;
customButtonItem.action = barButtonItem.action;
barButtonItem = customButtonItem;
[barButtonItem setTitle:@""];
//barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:[barButtonItem target] action:[barButtonItem action]];
//[barButtonItem setImage:[UIImage imageNamed:@"listing.png"]];
//Take this bar button item and put it on the left side of our nav item
[[self navigationItem] setLeftBarButtonItem:barButtonItem];
self.popoverController = pc;
}
This resulted in the image showing up when/where I want it to but the button has no border to it (despite me setting it to) and does not present a popover.
EDIT: I missed that this was splitView delegate and you were receiving the UIBarButtonItem from iOS. So what I would suggest you do is try the following.
Create your own buttonItem, and copy the target and selector from the one provided by the system. Create the new button item like this: