0

I have a UISearchBar which I am putting in UISearchDisplayController. Now, whenever I tap on the 'Search' button on the keyboard, it dismisses the keyboard. I am implementing the below method to stop searching whenever 'Search' button is tapped in which case I do not want to loose my keyboard also. Is there any way to instruct UISearchDisplayController to not dismiss the keyboard on tap on 'Search' button?

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar1 {
     if ([searchBar.text length] < 3){
          return;
     }
     else {
          // Do searching here or other stuffs
     }
}
Abhinav
  • 37,684
  • 43
  • 191
  • 309

1 Answers1

4

// This method return NO to not resign first responder

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar; 

So on your code it should be something like this to avoid the keyboard not to dismiss on search button tap only, but will dismiss on cancel, etc.

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar1 {
     isSearchTap = YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
   if(isSearchTap) {
    return NO;
   }
   return YES;
}
DLende
  • 5,162
  • 1
  • 17
  • 25
  • One small problem here. I think you need to set ```isSearchTap``` to NO in ```searchBarShouldEndEditing```. Otherwise you will never be able to stop editing of the search bar. – Jacksonh May 11 '14 at 01:36