搜索条是我们经常使用的一个控件;
iOS8之前,使用搜索采用的是:UISearchBar+UIDisplayController;
iOS8之后,采用的是UISearchController,看演示+上代码:
这里主要介绍简单创建,基本满足使用需求。
创建前的准备
需要先明白几个代理里面必须知道的代理回调
UISearchControllerDelegate
/*search bar 即将出现*/
- (void)willPresentSearchController:(UISearchController*)searchController;
/*search bar 已经出现*/
- (void)didPresentSearchController:(UISearchController*)searchController;
/*search bar 即将消失*/
- (void)willDismissSearchController:(UISearchController*)searchController;
/*search bar 已经消失*/
- (void)didDismissSearchController:(UISearchController*)searchController;
UISearchResultsUpdating
/*搜索条出现、消失、输入文字、删除文字时都会触发,且为必须实现的代理方法,即使你在里面什么都不写*/
- (void)updateSearchResultsForSearchController:(UISearchController*)searchController;
UISearchBarDelegate
这个代理里面的方法就很多了,而且大都很简单,在这里我只叙述两个。
/*点击取消按钮触发*/
- (void)searchBarCancelButtonClicked:(UISearchBar*)searchBar;
/*点击搜索按钮触发*/
- (void)searchBarSearchButtonClicked:(UISearchBar*)searchBar;
开始创建searchController
遵守代理
<UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating>
创建和基本属性
/*如果不需要指定显示在哪个控制器,则末尾的参数填写为nil就好,这样默认显示在当前的控制钱*/
self.searchController= [[UISearchController alloc]initWithSearchResultsController:nil];
/*遵守三个代理*/
self.searchController.delegate=self;
self.searchController.searchBar.delegate=self;
self.searchController.searchResultsUpdater=self;
/*提醒字眼*/
self.searchController.searchBar.placeholder=@"请输入关键字搜索";
/*searchController的背景视图颜色,如果不设置会显示你在搜索前的数据,是透过来的*/
self.searchController.view.backgroundColor= [UIColor whiteColor];
/*包着搜索框外层的颜色*/
self.searchController.searchBar.barTintColor= [UIColor grayColor];
//搜索时,背景变暗色
/*
self.searchController.dimsBackgroundDuringPresentation = NO;*/
/*搜索时,背景变模糊*/
/*
self.searchController.obscuresBackgroundDuringPresentation = NO;*/
/*点击搜索的时候,是否隐藏导航栏
如果有特殊需要,可以写在即将消失、即将出现的代理里面*/
/*
self.searchController.hidesNavigationBarDuringPresentation = NO;*/
/*这句如果不添加可能会导致点击search的时候search向上移动64,导致不可见,这句话是应用在控制器上面的*/
self.definesPresentationContext = YES;
self.tableHeaderView=self.searchController.searchBar;
如何table view的代理面区分是在搜索情形下还是正常情形下,在创建行数、高度、cell的地方区分就可以了
if(self.searchController.active) {
NSLog(@"搜索");
}else{
NSLog(@"正常table view");
}
回调以下几个代理,基本就可以了
- (void)updateSearchResultsForSearchController:(UISearchController*)searchController {
/*出现、消失、输入文字时都会触发*/
}
- (void)searchBarCancelButtonClicked:(UISearchBar*)searchBar {
/*点击取消按钮触发*/
}
- (void)searchBarSearchButtonClicked:(UISearchBar*)searchBar {
/*点击搜索按钮触发*/
}
-(void)willPresentSearchController:(UISearchController*)searchController {
/*search bar即将出现*/
}
-(void)willDismissSearchController:(UISearchController*)searchController {
/*search bar即将消失*/
}
如果进入控制则就要让search controller 呈现选中状态
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated]; [self performSelector:@selector(searchBarBecomeResponder)withObject:nil afterDelay:0];
}
- (void)searchBarBecomeResponder {
[self.searchController.searchBar becomeFirstResponder];
}
获取Search Controller 的searchBar里面的textField
self.searchBarTextField = [self.searchController.searchBar valueForKey:@"searchField"];
发现问题
-[TableView updateSearchResultsForSearchController:]: unrecognized selector sent to instance 0x7fff278aec00
这个问题是因为你遵守了UISearchResultsUpdating代理,但是没有实现- (void)updateSearchResultsForSearchController:(UISearchController*)searchController方法,实现这个方法即可;
我在创建的过程中,没有实现frame等方法,是因为我直接将searchBar加在table view header view上面。
网友评论