When I added the gesture to the UIScrollView subclass, I had problems with various gestures in the tree of my view, interfering with each other, for example, the ability to click on a subview, scroll through the view and have the keyboard dismiss in all cases. I came up with this solution, which can be configured from the superclass of UIScrollView or from the UIViewController .
The DismissKeyboardTapGesture class uses ARC, works with any text fields under the view, and does not take any clicks from subzones, such as buttons. It also uses the iOS7 scroll effect to disable the keyboard.
Setting from the superclass of UISScrollView:
_dismissKeyboard = [[DismissKeyboardTapGesture alloc] initWithView:self];
or from the UIViewController:
_dismissKeyboard = [[DismissKeyboardTapGesture alloc] initWithView:self.view];
Here is the class:
@interface DismissKeyboardTapGesture : NSObject <UIGestureRecognizerDelegate> @end @implementation DismissKeyboardTapGesture - (id)initWithView:(UIView *)view { self = [super init]; if (self) { UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)]; singleTap.cancelsTouchesInView = NO; singleTap.delegate = self; [view addGestureRecognizer:singleTap]; if ([view respondsToSelector:@selector(setKeyboardDismissMode:)]) {
source share