使用带有用于SplitView / Popover控件的图像或系统预设的UIBarButtonItem

时间:2012-08-03 21:00:25

标签: ios ipad ios-simulator

我正在优化iPad的应用程序,并希望使用带图像的UIBarButtonItem来呈现弹出窗口。如果我使用带有标题的按钮但我更喜欢使用我自己的图像或者使用UIBarButtonSystemItem提供的图像,那么我目前使用的代码效果很好。如果我根本没有设置按钮的标题,它就不会显示出来。如果我设置标题和图像,我会在真实硬件上获得标题,但图像显示在模拟器上。使用UIBarButtonSystemItem,我得到了我想要的按钮,但它也以横向模式显示(它不应该这样做,因为我正在使用景观的拆分视图)。

使用setImage,setTitle的任意组合或将按钮初始化为系统预设都无效。当检测到旋转到横向但它仍然在屏幕上时,我还尝试将系统预设的按钮设置为nil。我不确定如何从这里开始,但我真的想避免在这个按钮上使用字符串,而且我非常好奇地知道为什么会这样。

以下是代码:

#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;
}

更新:

我将此添加到我的viewDidLoad:

customButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"listing.PNG"]]];
customButtonItem.style = UIBarButtonItemStyleBordered;

并将willHideViewController更新为:

-(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;
}

这导致图像显示在我想要的时间/位置,但按钮没有边框(尽管我将其设置为)并且没有呈现弹出窗口。

1 个答案:

答案 0 :(得分:5)

编辑:我错过了这是splitView委托,你从iOS收到了UIBarButtonItem。所以我建议你做的是尝试以下内容。

创建您自己的buttonItem,并从系统提供的目标和选择器中复制目标和选择器。像这样创建新的按钮项:

UIBarButtonItem *newBut = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleBordered target: barButtonItem.target action: barButtonItem.action];
[[self navigationItem] setLeftBarButtonItem:newBut];