I have experienced that when you focus on UISearchBar, the keyboard occupied half of the screen. The question is, “Under what condition should the keyboard hide?”.

One of the solution is to follow the contact book, which is add an overlay button on top of it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
// add the button to the main view
UIButton *overlayButton = [[UIButton alloc] initWithFrame:self.view.frame];

// set the background to black and have some transparency
overlayButton.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3f];

// add an event listener to the button
[overlayButton addTarget:self action:@selector(hideKeyboard:) forControlEvents:UIControlEventTouchUpInside];

// add to main view
[self.view addSubview:overlayButton];
}

- (void)hideKeyboard:(UIButton *)sender
{
// hide the keyboard
[self.searchBar resignFirstResponder];
// remove the overlay button
[sender removeFromSuperview];
}

So now the keyboard will hide when you touched on the dark area.

Simple :)