Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Dismiss keyboard when touching outside of UITextField

I'm wondering how to make the keyboard disappear when the user touches outside of a UITextField.

like image 762
jonepatr Avatar asked Mar 15 '11 00:03

jonepatr


People also ask

How do you dismiss a keyboard from the tap?

Android devices have a solution; press the physical back button (provided on some mobile phones) or the soft key back button, and it closes the keyboard.

How do I dismiss the keyboard in iOS?

This is the quickest way to implement keyboard dismissal. Just set a Tap gesture on the main View and hook that gesture with a function which calls view. endEditing . Causes the view (or one of its embedded text fields) to resign the first responder status.

How do I dismiss the keyboard for a textField?

If you're supporting only iOS 15 and later, you can activate and dismiss the keyboard for a text field by focusing and unfocusing it. In its simplest form, this is done using the @FocusState property wrapper and the focusable() modifier – the first stores a Boolean that tracks whether the second is currently focused.


3 Answers

You'll need to add an UITapGestureRecogniser and assign it to the view, and then call resign first responder on the UITextField on it's selector.

The code:

In viewDidLoad

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

[self.view addGestureRecognizer:tap];

In dismissKeyboard:

-(void)dismissKeyboard 
{
    [aTextField resignFirstResponder];
}

(Where aTextField is the textfield that is responsible for the keyboard)

Swift 3 version looks like that

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard (_:)))
self.view.addGestureRecognizer(tapGesture)

For dismissKeyboard

@objc func dismissKeyboard (_ sender: UITapGestureRecognizer) {
    aTextField.resignFirstResponder()
}
like image 130
Jensen2k Avatar answered Oct 19 '22 16:10

Jensen2k


I mashed up a few answers.

Use an ivar that gets initialized during viewDidLoad:

UIGestureRecognizer *tapper;

- (void)viewDidLoad
{
    [super viewDidLoad];
    tapper = [[UITapGestureRecognizer alloc]
                initWithTarget:self action:@selector(handleSingleTap:)];
    tapper.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapper];
}

Dismiss what ever is currently editing:

- (void)handleSingleTap:(UITapGestureRecognizer *) sender
{
    [self.view endEditing:YES];
}
like image 21
7 revs, 5 users 93% Avatar answered Oct 19 '22 18:10

7 revs, 5 users 93%


Check this, this would be the easiest way to do that,

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
      [self.view endEditing:YES];// this will do the trick
}

Or

This library will handle including scrollbar auto scrolling, tap space to hide the keyboard, etc...

https://github.com/michaeltyson/TPKeyboardAvoiding

like image 106
Prasad De Zoysa Avatar answered Oct 19 '22 17:10

Prasad De Zoysa