从我在 SDK 中找到的 spliview master/detail 示例开始,我最终得到了一个 iPad 应用程序,它在横向的左侧有一个母版,而在纵向中,一个名为“Master”的按钮允许显示母版。
要显示此按钮,使用以下代码
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
barButtonItem.title = @"Master";
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
self.masterPopoverController = popoverController;
}
在我的应用中,主视图将是某种搜索 View ,所以我希望名为主视图的按钮显示系统提供的搜索图标。
首先,我根本不知道如何以编程方式(在运行时)将系统图标添加到按钮?我知道如何在开发时而不是在运行时在 Xcode 的 Storyboard 中选择它。
因为我不知道该怎么做,出于测试目的,我在栏的顶部添加了一个右栏按钮,我只是将其图像分配给主按钮,如下所示:
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
//barButtonItem.title = @"Master";
barButtonItem.image = searchBarButton.image;
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
self.masterPopoverController = popoverController;
}
结果是主按钮根本不显示。
我再说一遍,这只是为了测试目的,最后我想简单地分配系统提供的搜索图标。
所以我的两个问题是:
- 为什么不显示主按钮?我必须为此按钮使用“文本”吗
- 如何将系统提供的图标(在运行时)分配给此类按钮?
谁能帮帮我?
最佳答案
您需要初始化条形按钮项。您可以使用以下代码执行此操作,将目标和操作参数替换为您的目标和函数。
self.barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Master" style:UIBarButtonItemStylePlain target:self action:@selector(action)];
[self.navigationItem setLeftBarButtonItem:_barButtonItem animated:YES];
如果要使用系统项,请使用以下代码,将 initWithTitle 替换为 initWithBarButtonSystemItem。再次替换目标和操作参数。
self.barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(action)];
[self.navigationItem setLeftBarButtonItem:_barButtonItem animated:YES];
您还必须确保在@interface 下包含 barButtonItem。
@interface ViewController ()
@property UIBarButtonItem *barButtonItem;
@end
关于uibutton - 带有图标的 iOS ipad Split View主按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14252593/