Ios 使用带有图像或系统预设的UIBarButtonItem进行SplitView/Popover控制
我目前正在优化一个iPad应用程序,我想用一个带有图像的UIBarbuttonite来展示一个popover。如果我使用的是带有标题的按钮,那么我目前拥有的代码非常有用,但我更喜欢使用我自己的图像或UIBarButtonSystemItem提供的图像之一。如果我根本不设置按钮的标题,它就不会显示。如果我设置了一个标题和一个图像,我会在真实的硬件上得到标题,但是图像会显示在模拟器上。使用UIBarButtonSystemItem,我得到了我想要的按钮,但它也出现在横向模式下(它不应该这样做,因为我使用的是横向分割视图) 使用setImage、setTitle的任意组合或将按钮初始化为系统预设都不起作用。当检测到横向旋转时,我还尝试将系统预设的按钮设置为nil,但它仍保留在屏幕上。我不确定如何从这里开始,但我真的想避免使用这个按钮的字符串,而且我非常好奇知道为什么会发生这种情况 代码如下:Ios 使用带有图像或系统预设的UIBarButtonItem进行SplitView/Popover控制,ios,ipad,ios-simulator,Ios,Ipad,Ios Simulator,我目前正在优化一个iPad应用程序,我想用一个带有图像的UIBarbuttonite来展示一个popover。如果我使用的是带有标题的按钮,那么我目前拥有的代码非常有用,但我更喜欢使用我自己的图像或UIBarButtonSystemItem提供的图像之一。如果我根本不设置按钮的标题,它就不会显示。如果我设置了一个标题和一个图像,我会在真实的硬件上得到标题,但是图像会显示在模拟器上。使用UIBarButtonSystemItem,我得到了我想要的按钮,但它也出现在横向模式下(它不应该这样做,因为我
#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;
}
这导致图像在我希望的时间/地点显示,但按钮没有边框(尽管我将其设置为),并且不显示弹出框 EDIT:我没有注意到这是splitView委托,而您正在从iOS接收UIBarButtonItem。所以我建议你做的是尝试下面的方法 创建自己的buttonItem,并从系统提供的buttonItem复制目标和选择器。创建新的按钮项,如下所示:
UIBarButtonItem *newBut = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleBordered target: barButtonItem.target action: barButtonItem.action];
[[self navigationItem] setLeftBarButtonItem:newBut];
编辑:我没有注意到这是splitView委托,而您正在从iOS接收UIBarButtonItem。所以我建议你做的是尝试下面的方法 创建自己的buttonItem,并从系统提供的buttonItem复制目标和选择器。创建新的按钮项,如下所示:
UIBarButtonItem *newBut = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleBordered target: barButtonItem.target action: barButtonItem.action];
[[self navigationItem] setLeftBarButtonItem:newBut];
我试过这个,但我不确定我做得是否正确。你介意检查我对原始帖子的更新吗?再次检查按钮上是否设置了目标和操作(记录)。可能不可能有带有边框的自定义视图样式-我记得读过,但现在找不到源。启动应用程序时,目标和操作显示为null,但在设备旋转后设置。尽管设置好了,但点击图像时什么也不会发生。太棒了。非常感谢。最后一个障碍是确保初始化willHideViewController中的新按钮,但这成功了。我尝试了这个,但我不确定是否正确。你介意检查我对原始帖子的更新吗?再次检查按钮上是否设置了目标和操作(记录)。可能不可能有带有边框的自定义视图样式-我记得读过,但现在找不到源。启动应用程序时,目标和操作显示为null,但在设备旋转后设置。尽管设置好了,但点击图像时什么也不会发生。太棒了。非常感谢。最后一个障碍是确保初始化willHideViewController中的新按钮,但这成功了。