self.searchController= [[UISearchController alloc] initWithSearchResultsController: nil];
[self.searchController.searchBar sizeToFit];
self.searchController.searchBar.backgroundImage= [UIImage createBgWithColor:[UIColor cyanColor]];
[self.searchController.searchBar setBackgroundImage:[UIImage createBgWithColor:[UIColor greenColor]] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
self.searchController.searchBar.barTintColor = UIColorHex(f4f4f4);
self.searchController.delegate = self;
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation=NO;
self.definesPresentationContext=YES;
self.contactsTableView.tableHeaderView=self.searchController.searchBar;
当设置成UIBarPositionBottom时,如图
当设置成UIBarPositionTop时,如图
当设置成UIBarPositionTopAttached时,如图
--------------------------------------------------------------------------------------------------------------------------------------
self.searchBar= [[UISearchBaralloc]init];
_searchBar.tintColor=[UIColorblueColor];//解决光标不显示问题
_searchBar.delegate=self;
_searchBar.placeholder=@"搜索";
self.navigationItem.titleView=self.searchBar;
#pragma mark - UISearchBarDelegate Methods
- (BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar{
NSLog(@"11--searchBarShouldBeginEditing");
returnYES;
}// return NO to not become first responder
- (void)searchBarTextDidBeginEditing:(UISearchBar*)searchBar{
NSLog(@"22--searchBarTextDidBeginEditing");
}// called when text starts editing
- (BOOL)searchBar:(UISearchBar*)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text {
NSLog(@"33--shouldChangeTextInRange");
returnYES;
}// called before text changes
- (void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)searchText{
NSLog(@"44--textDidChange");
}// called when text changes (including clear)
- (BOOL)searchBarShouldEndEditing:(UISearchBar*)searchBar{
NSLog(@"是否允许取消搜索--searchBarShouldEndEditing:%s",__func__);
returnYES;
}// return NO to not resign first responder
- (void)searchBarTextDidEndEditing:(UISearchBar*)searchBar{
NSLog(@"结束搜索searchBarTextDidEndEditing:%s",__func__);
}// called when text ends editing
- (void)searchBarSearchButtonClicked:(UISearchBar*)searchBar {
NSLog(@"键盘按钮\"查找\"被点击了searchBarSearchButtonClicked:%s",__func__);
}// called when keyboard search button pressed
- (void)searchBarBookmarkButtonClicked:(UISearchBar*)searchBar {
NSLog(@"键盘按钮\"书签\"被点击了searchBarBookmarkButtonClicked:%s",__func__);
}// called when bookmark button pressed
- (void)searchBarCancelButtonClicked:(UISearchBar*)searchBar {
NSLog(@"键盘按钮\"Cancel\"被点击了searchBarCancelButtonClicked:%s",__func__);
}// called when cancel button pressed
- (void)searchBarResultsListButtonClicked:(UISearchBar*)searchBar {
NSLog(@"键盘按钮\"ResultsList\"被点击了searchBarResultsListButtonClicked:%s",__func__);
}// called when search results button pressed
- (void)searchBar:(UISearchBar*)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
NSLog(@"selectedScopeButtonIndexDidChange:%s",__func__);
}//告诉表格,你选择的是那个按钮。