How to hide the keyboard when touching screen (search bar)
Asked Answered
S

4

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.

Sapwood answered 6/6, 2013 at 7:53 Comment(2)
Have you tried this answer by Jensen2k: #5306740Verminous
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!Sapwood
M
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];
}
Microsporophyll answered 6/6, 2013 at 8:21 Comment(4)
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.Sapwood
Yes I mean the UISearchBar. Difine your UISearchBar IBOutLet in you interface h file.Microsporophyll
Yeah I forgot to type: @synthesize searchBar; in my .m file. Now it works. Thanks :DSapwood
You can choose this as an answer if it solved. It will help other to find out the answer. Thanks.Microsporophyll
W
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.

Wetnurse answered 6/6, 2013 at 8:2 Comment(1)
Tried this also a couple of times. I get the following error: Unkown receiver 'searchBar' did you mean UIDearchBar?Sapwood
C
0

Try to use this one

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
  [self.view endEditing:YES];
}
Corder answered 6/6, 2013 at 8:18 Comment(2)
I don't get any errors, but it don't work either. Happens nothing. Thanks anyway!Sapwood
if you are using scrollview inside view then it won't work otherwise it will work.Corder
F
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];
}
Footplate answered 11/5, 2016 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.