Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当在导航栏上单击搜索按钮时,要在导航栏上添加搜索栏吗_Ios_Uitableview_Uisearchbar_Navigationbar - Fatal编程技术网

Ios 当在导航栏上单击搜索按钮时,要在导航栏上添加搜索栏吗

Ios 当在导航栏上单击搜索按钮时,要在导航栏上添加搜索栏吗,ios,uitableview,uisearchbar,navigationbar,Ios,Uitableview,Uisearchbar,Navigationbar,我正在使用UItableView控制器,无法将搜索栏添加到导航栏。我希望在单击导航栏上的“搜索”按钮时搜索栏出现。如何在导航栏上添加搜索按钮和搜索栏 对于搜索按钮,如果需要,可以使用UIBarButtonSystemItemSearch self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarB

我正在使用UItableView控制器,无法将搜索栏添加到导航栏。我希望在单击导航栏上的“搜索”按钮时搜索栏出现。如何在导航栏上添加搜索按钮和搜索栏

对于搜索按钮,如果需要,可以使用UIBarButtonSystemItemSearch

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
                               initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
                               target:self
                               action:@selector(actionSearch:)];
您可以将搜索栏设置为导航栏的标题视图

self.navigationItem.titleView = mySearchBar;

在单击=>.m文件时添加此按钮:

 UISearchBar  *sBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,10,self.navigationController.navigationBar.bounds.size.width,self.navigationController.navigationBar.bounds.size.height/2)];
sBar.delegate = self;
[self.navigationController.navigationBar addSubview:sBar];
并添加这个=>.h文件

@interface ViewController : UIViewController<UISearchBarDelegate>

要使搜索栏像这样,您需要在导航栏控制器中插入按钮,并将背景图像设置为search.pngyour image。因此,当用户单击此按钮时,将目标设置为SearchBar将被打开。请检查以下代码以供参考

首先,在.h文件中设置委托方法

现在,您必须将searchcontroller设置为clickme按钮方法

- (IBAction)clickme:(id)sender{
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 300, 44.0)];
searchBar.autoresizingMask =0;
searchBar.delegate = self;
searchBar.placeholder = @"Search for items...";
searchBar.showsScopeBar=YES;

UIView *searchBarWrapper = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
searchBarWrapper.autoresizingMask = 0;
[searchBarWrapper addSubview:searchBar];

self.searchItem = [[UIBarButtonItem alloc] initWithCustomView:searchBarWrapper];
self.tabBarController.navigationItem.leftBarButtonItem = self.searchItem;

self.navigationItem.rightBarButtonItem = nil;
self.navigationItem.titleView = nil;

  ////////////// ~ Search Display Controller as Object ~/////////////////////////////

self.d1 = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
self.d1.delegate = self;
self.d1.searchResultsDataSource = self;
self.d1.searchResultsDelegate = self;
self.d1.searchResultsTableView.rowHeight = 40;
self.d1.displaysSearchBarInNavigationBar = YES;
self.searchBar.translucent = NO;
self.searchBar.barTintColor = [UIColor grayColor];
self.d1.searchBar.tintColor = [UIColor blueColor];

[searchBar sizeToFit];

}
当你点击搜索图标时


你是在使用故事板还是在代码中使用它?我在使用故事板
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(279,9,25, 25)];
[btn setImage:[UIImage imageNamed:@"search"] //put here your searchimage
forState:UIControlStateNormal];
[btn setTitle:@"" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(clickme:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barbtn=[[UIBarButtonItem alloc]initWithCustomView:btn];
self.tabBarController.navigationItem.rightBarButtonItem=barbtn;
[self.tabBarController.navigationController.navigationBar setHidden:NO];
- (IBAction)clickme:(id)sender{
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 300, 44.0)];
searchBar.autoresizingMask =0;
searchBar.delegate = self;
searchBar.placeholder = @"Search for items...";
searchBar.showsScopeBar=YES;

UIView *searchBarWrapper = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
searchBarWrapper.autoresizingMask = 0;
[searchBarWrapper addSubview:searchBar];

self.searchItem = [[UIBarButtonItem alloc] initWithCustomView:searchBarWrapper];
self.tabBarController.navigationItem.leftBarButtonItem = self.searchItem;

self.navigationItem.rightBarButtonItem = nil;
self.navigationItem.titleView = nil;

  ////////////// ~ Search Display Controller as Object ~/////////////////////////////

self.d1 = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
self.d1.delegate = self;
self.d1.searchResultsDataSource = self;
self.d1.searchResultsDelegate = self;
self.d1.searchResultsTableView.rowHeight = 40;
self.d1.displaysSearchBarInNavigationBar = YES;
self.searchBar.translucent = NO;
self.searchBar.barTintColor = [UIColor grayColor];
self.d1.searchBar.tintColor = [UIColor blueColor];

[searchBar sizeToFit];

}