Keyboard rejection from UITextField, UITextView as a sub-task of UIScrollView?

I have an application with a UIScrollView added as a subtitle of a UIView . I added UITextField , UITextview as a subView of UIScrollView . I want to remove the keyboard when I enter the scroll. How can i do this?

+3
source share
4 answers

Just add UITapGestureRecognizer

 - (void)viewDidLoad { [super viewDidLoad]; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)]; [scr addGestureRecognizer:singleTap]; } - (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture { //Get touch point CGPoint touchPoint=[gesture locationInView:scr]; //Hide keyBoard [self.view endEditing:YES]; } 
+10
source

In iOS 7, you can easily achieve this.

 scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive; 
+2
source

Try it,

 - (void)viewDidLoad { [super viewDidLoad]; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; tapGesture.cancelsTouchesInView = NO; [scrollView addGestureRecognizer:tapGesture]; [tapGesture release]; } -(void)dismissKeyboard { [txtNotes resignFirstResponder]; [textView resignFirstResponder]; } 
0
source

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:)]) { // Bonus effect to dismiss keyboard by scrolling ((UIScrollView *)view).keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive; } } return self; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { // Don't stop any existing gestures in our view from working if (otherGestureRecognizer.view == gestureRecognizer.view) { return YES; } return NO; } - (void)singleTap:(UIGestureRecognizer*)gestureRecognizer { // Close keyboard for any text edit views that are children of the main view [gestureRecognizer.view endEditing:YES]; } @end 
0
source

All Articles