Dismissing the keyboard in a UIScrollView
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 buttonstapGesture.cancelsTouchesInView = NO; [pageScrollView addGestureRecognizer:tapGesture];[tapGesture release];...// method to hide keyboard when user taps on a scrollview-(void)hideKeyboard{ [myTextFieldInScrollView resignFirstResponder];}
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:
- Drag a Tap Gesture Recognizer onto your scrollView
- Only one line in the action ——
scrollView.endEditing(true)
. If you are using Objective-C,[self.scrollView endEditing: YES];