How to hide the keyboard programmatically in iphone
Asked Answered
A

6

12

How to hide the keyboard programmatically in iphone?

Arsphenamine answered 6/4, 2010 at 17:30 Comment(0)
O
22

Tell the UIResponder subclass that is currently first responder to resign its first responder status:

[responder resignFirstResponder];
Ogrady answered 6/4, 2010 at 17:41 Comment(0)
T
20
[textFieldName resignFirstResponder];
Traumatize answered 7/4, 2010 at 8:40 Comment(0)
A
4

It's easy:

ObjC

[[[UIApplication sharedApplication] keyWindow] endEditing:YES];

Swift

UIApplication.shared.keyWindow?.endEditing(true)

take a look at UIView Class Reference for endEditing. Causes the view (or one of its embedded text fields) to resign the first responder status. And keyWindow is the only window that receives keyboard events, so this solution is guarantied to always work.

Arratoon answered 16/4, 2013 at 10:20 Comment(0)
O
2

Call this in your ViewController

[self.view endEditing:YES];
Omeara answered 12/12, 2012 at 13:0 Comment(0)
R
0

If you are using textview then

- (BOOL)textView:(UITextView *)textView
 shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
  if ([text isEqualToString:@"\n"])
 {
    [textView resignFirstResponder];
    [self keyboardWillHide];
 }
}

and if you are using textfield then

-(BOOL)textFieldShouldReturn:(UITextField*)textField;
 {

[textField resignFirstResponder];

 }
Robinet answered 20/7, 2013 at 7:5 Comment(0)
B
0

Here is the swift version:

UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)
Brittneybrittni answered 10/11, 2015 at 5:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.