Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 302
Rick Avatar asked Jun 06 '13 07:06

Rick


2 Answers

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];
}
like image 188
iMubarak Avatar answered Oct 14 '22 05:10

iMubarak


- (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.

like image 36
John Doe Avatar answered Oct 14 '22 05:10

John Doe