4

I went through all the solutions but none are working. I am developing an app for iOS 6, ipad. I want the keyboard to go away when user touches outside (on scrollview)...

Siva
  • 1,848
  • 2
  • 23
  • 40

8 Answers8

6

Give 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];
}
Mano
  • 670
  • 1
  • 9
  • 28
1

Try to use this one. And i hope it 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];
    }
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
1

set the scrollview Delegate to self

self.scrollView.delegate=self;

then

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{


if (sTitle.isFirstResponder) {
    [sTitle resignFirstResponder];
}
}
Ankur
  • 5,086
  • 19
  • 37
  • 62
ajithmn89
  • 249
  • 3
  • 15
1

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];
}
Ajay Chaudhary
  • 1,993
  • 15
  • 23
0
  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
 }
SAMIR RATHOD
  • 3,512
  • 1
  • 20
  • 45
0

if you are using xib then just connect a tapRecognizer to your scrollview and then create tapRecognizer's selector event to [self.view endEditing:YES]

Vaibhav Saran
  • 12,848
  • 3
  • 65
  • 75
0
add custom button on scrollview and
-(IBAction)btn:(id)sender
{
  [txt resignfirstresponder];
}

enjoy it!!
Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42
0
UITapGestureRecognizer *tapScroll = [[UITapGestureRecognizer alloc]initWithTarget:self     action:@selector(Click)];
ScrollClick.cancelsTouchesInView = NO;
[YOUR scrollview addGestureRecognizer:ScrollClick];


- (void)Click
{
 [self.view endEditing:YES];
}

Try this one. And i hope it will be helpful For You . enjoy it !

D.M Patel
  • 145
  • 1
  • 7