Reputation: 1531
I am using
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.view.endEditing(true)
}
in order to dismiss the keyboard when the user taps somewhere, but it won't work. And I don't get any errors
My VC is built up: View > Visual effect view > view > scroll view > uiButton (it covers the screen and works as a exit/back button to prev VC) > Designable view (Here is where my login form is)
Upvotes: 0
Views: 1061
Reputation: 6212
In interface builder, drag a tap gesture recognizer
on the view that you want it to dismiss the keyboard after a tap gesture or you can add the gesture by code :
-(void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedOutside:)];
gestureRecognizer.cancelsTouchesInView = NO;
[scrollView addGestureRecognizer:gestureRecognizer];
}
Add an IBAction
to the tap gesture recognizer
for example - (IBAction)tappedOutside:(id)sender;
In the implementation file add :
-(IBAction)tappedOutside:(id)sender {
[self.view endEditing:YES];
}
Swift:
var tap : UITapGestureRecognizer = UITapGestureRecognizer(target: self,action: "dismissKeyboard")
self.view.addGestureRecognizer(tap)
func dismissKeyboard() {
self.view.endEditing(true)
}
Upvotes: 2
Reputation: 1442
First, Add delegate to your textfiled
yourtextfiled.delegate=self
Second, Set user interaction enable for Your UIScrollview and View
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
yourtextfiled.resignFirstResponder()
}
Upvotes: 0
Reputation: 10060
take the textField that opened that keyboard and try
textField.resignFirstResponder()
Upvotes: 1