How to hide the keyboard by touching the Background scroll screen?

I went through all the solutions, but no one works. I am developing an application for iOS 6, ipad. I want the keyboard to disappear when the user touches out (on scrollview) ...

+4
source share
8 answers

Enter the following code in viewDidLoad

-(void) ViewDidLoad { UITapGestureRecognizer *tapScroll = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapped)]; tapScroll.cancelsTouchesInView = NO; [scrollview addGestureRecognizer:tapScroll]; } 

And define the function as follows

 - (void) tapped { [self.view endEditing:YES]; } 
+6
source

Try using this one. And I hope this will be helpful.

  - (void)viewDidLoad { [super viewDidLoad]; UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard:)]; gestureRecognizer.delegate = self; [scrollView addGestureRecognizer:gestureRecognizer]; } -(void) hideKeyBoard:(UIGestureRecognizer *) sender { [self.view endEditing:YES]; } 
+1
source

set scrollview delegate for self

 self.scrollView.delegate=self; 

then

 -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ if (sTitle.isFirstResponder) { [sTitle resignFirstResponder]; } } 
+1
source

You can use this code to hide the keyboard:

 -(void)viewDidLoad { [super viewDidLoad]; UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideTheKeyBoard:)]; gestureRecognizer.delegate = self; [scrollView addGestureRecognizer:gestureRecognizer]; } -(void) hideTheKeyBoard:(UIGestureRecognizer *)sender { [self.view endEditing:YES]; } 
+1
source
  UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)]; [tapRecognizer setNumberOfTapsRequired:1]; [tapRecognizer setDelegate:self]; [scrollview addGestureRecognizer:tapRecognizer]; -(void)tapped:(id)sender { [textField resignFirstResponder]; // your code what you want } 
0
source

if you use xib just connect tapRecognizer to scrollview and then create tapRecognizer's event for selector [self.view endEditing:YES]

0
source
 add custom button on scrollview and -(IBAction)btn:(id)sender { [txt resignfirstresponder]; } enjoy it!! 
0
source
 UITapGestureRecognizer *tapScroll = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Click)]; ScrollClick.cancelsTouchesInView = NO; [YOUR scrollview addGestureRecognizer:ScrollClick]; - (void)Click { [self.view endEditing:YES]; } 

Try it. And I hope that it will be useful for you. enjoy it!

0
source

Source: https://habr.com/ru/post/1481349/


All Articles