Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 894
ios developer Avatar asked Nov 29 '22 16:11

ios developer


2 Answers

Implement a method from UISearchBarDelegate:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
}
like image 142
Baby Groot Avatar answered Dec 07 '22 01:12

Baby Groot


For Swift

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
}
like image 24
Mostafa Sultan Avatar answered Dec 07 '22 00:12

Mostafa Sultan