This failure of Apple to allow user to dismiss keyboard is taking its toll...
I have managed to add code to do this on a few views but now I have a ViewController with a TextView and a couple of TableViews.
After I use a Tap Gesture Recognizer to dismiss the keyboard, the taps on the table cells don't trigger. The code is as follows.
func tap(gesture: UITapGestureRecognizer) {
if gesture.view!.tag != 33 {
self.view.endEditing(true)
titleView.resignFirstResponder()
}
}
This might be belt & braces but I was trying to see if/what might work. (Tag 33 is the actual TextView)
The table cell is triggered in the usual way and without the tap gesture code it works fine.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: false)....
An IBAction tap on a button seems to work but not the cell trigger.
As is often the case here, the process of posting a question leads to a self-answer....
If using TableView cell taps (and I guess in general), add the following line to the gesture setup.
gestureRecognizer.cancelsTouchesInView = false
The solution in more detail can be found here. Dismiss keyboard by touching background of UITableView
Hey @RobertyBob adding gesture recognizer does not allow to execute did selectRowForIndex Path.
To dismiss keyboard on I would suggest you to add code to your controller file.
Note that: Conform Protocol UITextFieldDelegate and set all textfields.delegate = self.
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
1. Select TextField
2. Goto attribute inspector and choose keyboard type to default and Return Key to Done
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.