Alright, I have a couple of UITextFields
and UITextViews
inside a UIScrollView
, and I\'d like to set the keyboard to disappear wheneve
Try This
[self.selectedViewController.view endEditing:YES]
;
Here is the cleanest way to achieve this in iOS 7.0 and above.
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
Or
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
In Swift:
scrollView.keyboardDismissMode = .onDrag
Or
scrollView.keyboardDismissMode = .interactive
Bit late but if anyone else is searching an answer to this problem, this is how I have gone about solving it:
1) Create a tap gesture recognizer with a target callback method to dismiss your keyboard using resignFirstResponder on all your fields.
2) Add the tap gesture to the scrollview.
Here's an example:
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
// prevents the scroll view from swallowing up the touch event of child buttons
tapGesture.cancelsTouchesInView = NO;
[pageScrollView addGestureRecognizer:tapGesture];
[tapGesture release];
...
// method to hide keyboard when user taps on a scrollview
-(void)hideKeyboard
{
[myTextFieldInScrollView resignFirstResponder];
}
Try this scroll view delegate method -
link delegate in IB to scroll view and then cop this code (modify as per your need).
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
//sample code
[challengeABallotComponent.voterNameTextField resignFirstResponder];
[challengeABallotComponent.ballotNumberTextField resignFirstResponder];
[locationInformation.pollingLocation resignFirstResponder];
}
This should work. You can try other delegate methods too like
-(void)scrollViewDidScroll: (UIScrollView *)scrollView
{
//do your stuff
}
Although the essence is the same, I prefer less code.
Setting the keyboard to disappear when the scrollView is scrolled in Attributes inspector:
Then disappear keyboard when scrollView is tapped:
scrollView.endEditing(true)
. If you are using Objective-C, [self.scrollView endEditing: YES];
extension UIView{
//Set tag via storyboard
func keyboardDissmissInteractiveMode(_ tag:Int){
if let scrollView = self.viewWithTag(tag) as? UIScrollView{
scrollView.keyboardDismissMode = .interactive
}
if let tableview = self.viewWithTag(tag) as? UITableView{
tableview.keyboardDismissMode = .interactive
}
}
func keyboardDissmissOnDragMode(_ tag:Int){
if let scrollView = self.viewWithTag(tag) as? UIScrollView{
scrollView.keyboardDismissMode = .onDrag
}
if let tableview = self.viewWithTag(tag) as? UITableView{
tableview.keyboardDismissMode = .onDrag
}
}
func keyboardDissmissInteractiveMode(_ view:UIView){
if let scrollView = view as? UIScrollView{
scrollView.keyboardDismissMode = .interactive
}
if let tableview = view as? UITableView{
tableview.keyboardDismissMode = .interactive
}
}
func keyboardDissmissOnDragMode(_ view:UIView){
if let scrollView = view as? UIScrollView{
scrollView.keyboardDismissMode = .onDrag
}
if let tableview = view as? UITableView{
tableview.keyboardDismissMode = .onDrag
}
}
}