Search code examples
iphoneuisearchbaruibarbuttonitemuitoolbar

Hide the keybord on clicking enter button or search button


I've added the UISearchBar button inside the UIBarButtonItem inside the toolbar button.

In the below form:

// search bar
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 260, 44)];
UIBarButtonItem *searchBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
[searchBar release];

[buttons addObject:searchBarButtonItem];

Now the problem in when I click on the UISearchBar keyboard appears. I'd like to hide the keyboard on clicking in enter or search button. How can i do this?


Solution

  • Implement a method from UISearchBarDelegate:

    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
        [searchBar resignFirstResponder];
    }