Hiding Keyboard When User Taps Anywhere Else on the Screen | iOS Programmer Guide

We can hide the keyboard when user taps anywhere else on the screen. For example, I have a textbox on a screen when I click on the textbox keyboard will appear and when I click anywhere else on the screen, the keyboard should disappear.

To achieve hide the keyboard when user taps anywhere else on the screen of iOS App add the below code snippet.

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