-3

I am new in iOS development. I want to hide the keyboard when tapping outside of a TextField. My TextField is in a cell from an UITableView.

I have tried to follow some of those links, however without any success--

Dismiss keyboard on touch anywhere outside UITextField

Dismiss keyboard by touching background of UITableView

Hide keyboard when scroll UITableView

I am trying to find the simplest way possible. Thanks in advance

Community
  • 1
  • 1
Eric
  • 45
  • 1
  • 5

9 Answers9

8

This is the simplest way to dismiss keyboard

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [tableView addGestureRecognizer:gestureRecognizer];
}
- (void)hideKeyboard
{
    [self.view endEditing:YES];
}
Arbab Khan
  • 98
  • 5
3

It's not about touch, only working when scroll

TableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

also have

UIScrollViewKeyboardDismissModeInteractive // the keyboard follows the dragging touch off screen, and may be pulled upward again to cancel the dismiss

Mathi Arasan
  • 869
  • 2
  • 10
  • 32
0

you can use Tap gesture to hide keyboard.

- (void) tapGesture : (UIGestureRecognizer *) gestureRecognizer {   
    for (UIView *subview in view.subviews) {
        if([subview isKindOfClass : [UITextField class]] ) {
            UITextField *tf = (UITextField *) subview;
            [tf resignFirstResponder];
        } 
    }
}
Sujay
  • 2,510
  • 2
  • 27
  • 47
sandeep
  • 25
  • 1
  • 9
0

Add delegate class UITextFieldDelegate

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
Sujay
  • 2,510
  • 2
  • 27
  • 47
0

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

Naldhelaan
  • 390
  • 4
  • 13
0

Try This code Write following code in viewDidLoad and add UIGestureRecognizerDelegate in .h file.

    UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
                                        action:@selector(handleSingleTap:)];
[singleFingerTap setDelegate:self];
[self.view addGestureRecognizer:singleFingerTap];

// Listen for keyboard appearances and disappearances

[[NSNotificationCenter defaultCenter] addObserver:self 
                                     selector:@selector(keyboardDidShow:)
                                         name:UIKeyboardDidShowNotification
                                       object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(keyboardDidHide:)
                                         name:UIKeyboardDidHideNotification
                                       object:nil];

Delegates of keyboard Appearances and disappearances

- (void)keyboardDidShow: (NSNotification *) notif{
 // Do something here
  tblview.tag = 1;
}
- (void)keyboardDidHide: (NSNotification *) notif{
// Do something here
tblview.tag = 0;
}

UITapGestureRecognizer event function for hide keyboard

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
   blview.tag = 0;
  [self.view endEditing:YES];
 }

UIGestureRecognizer delegate

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if(tblview.tag == 1){
    return TRUE;
}
else{
    return FALSE;
   }
}
Om Prakash
  • 9,191
  • 1
  • 12
  • 20
0

I am using the solution in two parts:

To dismiss keyboard on tableview/collectionview tap:

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
gestureRecognizer.cancelsTouchesInView= NO;
[self.collectionView addGestureRecognizer:gestureRecognizer];

(Don't forget cancelsTouchesInView set to NO to get touch event of tableview/collection view)

To dismiss keyboard on scroll (as tableview/collectionview are subclass of UIScrollView):

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
     [self.view endEditing:YES];
}

Hope it helps somebody.

atulkhatri
  • 10,896
  • 3
  • 53
  • 89
0

This will help you..

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ 
 [self.view endEditing:YES]; 
}
Sabby
  • 403
  • 3
  • 15
0

The easiest way is to alloc a tap Gesture in viewDidLoad and then hide keyboard

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [_tableView addGestureRecognizer:gestureRecognizer];
}
- (void)hideKeyboard
{
    [self.view endEditing:YES];
}

Or on github you certainly found a library that hide your keyboard

Berry
  • 1