首先头文件要引用UISearchBarDelegate
设置属性@property (nonatomic,strong)UISearchController * searchController;
在viewload里面设置:
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];//初始化
[self.searchController.searchBar sizeToFit];//自动适应大小
self.tableView.tableHeaderView = self.searchController.searchBar;//tableview的头视图就是searchBar
self.searchController.dimsBackgroundDuringPresentation = YES;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
方法:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
//点击搜索后调用的方法
代码不全,一般这里要调用接口,用srVC的数组接收数据,再转过去到srVC
SearchResultViewController * srVC = [[SearchResultViewController alloc] init];
srVC.searchText = searchBar.text;
[self.navigationController pushViewController:srVC animated:YES];
}