Search code examples
iphoneobjective-ccocoa-touchuisearchbaruisearchdisplaycontroller

Stopping UISearchDisplayController to dismiss the searchbar keyboard


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
     }
}

Solution

  • // 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;
    }