How to hide keyboard on touch UITableView in iOS Obj-C

随声附和 提交于 2019-12-23 07:09:25

问题


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


回答1:


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];
}



回答2:


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




回答3:


Add delegate class UITextFieldDelegate

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}



回答4:


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.




回答5:


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];
        } 
    }
}



回答6:


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;
   }
}



回答7:


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.




回答8:


This will help you..

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ 
 [self.view endEditing:YES]; 
}



回答9:


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



来源:https://stackoverflow.com/questions/31048510/how-to-hide-keyboard-on-touch-uitableview-in-ios-obj-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!