8

The keyboard hides when I click search or when I click on cancel. But I want also that the keyboard hides when I click somewhere on the screen. I found several tutorials for the textfield, but we are using the search bar.

Can someone tell me how to do this?

Thanks.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Rick
  • 85
  • 1
  • 1
  • 4
  • Have you tried this answer by Jensen2k: http://stackoverflow.com/questions/5306240/iphone-dismiss-keyboard-when-touching-outside-of-textfield – mfaerevaag Jun 06 '13 at 07:56
  • Thanks for your reply. Yeah i tried that one too. Problem is that i don't have a textfield where the keyboard is attached too. The search bar calls the keyboard so i have to resign it with the search bar. Do you know another solution? Thanks anyway! – Rick Jun 06 '13 at 08:02

4 Answers4

29

Try This

in your .h file add UISearchBar

@property (strong, nonatomic) IBOutlet UISearchBar *searchBar; 

in your .m file

- (void)viewDidLoad
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)];

    [self.view addGestureRecognizer:tap];
}


- (void) dismissKeyboard
{
    // add self
    [self.searchBar resignFirstResponder];
}
iMubarak
  • 462
  • 5
  • 13
  • Tried it first part didn't give any errors, second part says again: 'Unkown receiver 'searchBar'; did you mean UISearchBar?' Thanks for the help. I think this is close. – Rick Jun 06 '13 at 08:33
  • Yes I mean the UISearchBar. Difine your UISearchBar IBOutLet in you interface h file. – iMubarak Jun 06 '13 at 09:04
  • Yeah I forgot to type: @synthesize searchBar; in my .m file. Now it works. Thanks :D – Rick Jun 06 '13 at 09:07
  • You can choose this as an answer if it solved. It will help other to find out the answer. Thanks. – iMubarak Jun 06 '13 at 09:10
1
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   [yourTextField1 resignFirstResponder];
   [yourTextField2 resignFirstResponder];
   [yourTextField3 resignFirstResponder];
   [yourSearchBar resignFirstResponder];
   //etc
}

But probably you need to check where are you touching at since you don't want to hide the keyboard if you're touching on a text input or search box.

John Doe
  • 123
  • 4
  • Tried this also a couple of times. I get the following error: Unkown receiver 'searchBar' did you mean UIDearchBar? – Rick Jun 06 '13 at 08:04
0

Try to use this one

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
  [self.view endEditing:YES];
}
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

You could add a UITapGestureRecognizer to dismiss your keyboard.

- (void)viewDidLoad {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:tap];
}

- (void) dismissKeyboard {
     [self.view endEditing:YES];
}
Aleksander Azizi
  • 9,829
  • 9
  • 59
  • 87