Swift - Tap gesture to dismiss keyboard UITableView
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: Selector("hideKeyboard"))
tapGesture.cancelsTouchesInView = true
tableView.addGestureRecognizer(tapGesture)
}
func hideKeyboard() {
tableView.endEditing(true)
}
Translating Objective-C code to Swift is not really hard. You just require a basic knowledge in both languages. If you're new to programming I guess you should familiarise with yourself with the basics first.
How to dismiss keyboard in a tableView after typing in textview
You can use the UITableViewDelegate
which conforms to the UIScrollViewDelegate
to implement:
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
dismissKeyboard()
}
func dismissKeyboard(){
self.view.endEditing(true)
}
//Add to viewDidLoad:
var tapGesture = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
tableView.addGestureRecognizer(tapGesture)
//Or since you wanted to dismiss when another cell is selected use:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
dismissKeyboard()
}
Dismiss Keyboard when touch began on UITableView and GoogleMap's View
Try creating a subclass of UITableView like so:
subclassTB.h
#import
@interface subClassTB : UITableView
@end
subclassTB.m
#import "subclassTB.h"
@implementation subClassTB
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// do your stuff here
//NSLog(@"tap registered");
[super touchesBegan:touches withEvent:event];
}
@end
and in your current controller initialize the table by using
subclassTB *yourtable = [[subclassTB alloc]initWithFrame:youFrame style:yourStyle];
How to hide keyboard on touch UITableView in iOS Obj-C
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];
}
how to dismiss keyboard iOS on a table view
You should add a tap gesture recognizer to your table view that closes the keyboard when it's fired:
//Somewhere in setup
UITapGestureRecognizer *viewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyboard)];
viewTap.cancelsTouchesInView = FALSE;
[myTableView addGestureRecognizer:viewTap];
//Somewhere else in your class
-(void)closeKeyboard {
[self.view endEditing:TRUE];
}
TableView taps fail after gesture to dismiss keyboard
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
Related Topics
How to Pass Data Between View Controllers in Swift
Nslog on Devices in iOS 10/Xcode 8 Seems to Truncate - Why
How to Test Apple Push Notification Service Without an Iphone
I Have Real Misunderstanding With Mfmailcomposeviewcontroller in Swift (Ios8) in Simulator
Ios7 Uitextview Contentsize.Height Alternative
How to Set Uiview Border Properties from Interface Builder
How to Use Safe Area Layout Programmatically
Uiscrollview Scroll to Bottom Programmatically
How to Parse Json Response from Alamofire API in Swift
Sorting Array Alphabetically With Number
Uiwebview: Html5 Audio Pauses in iOS 6 When App Enters Background
How to Support Universal Links in iOS App and Setup Server For It
Ios9 Getting Error "An Ssl Error Has Occurred and a Secure Connection to the Server Cannot Be Made"
Drawing a Route in Mapkit in Ios