Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resign First Responder on ScrollView Touch

How can I hide the keyboard on ScrollView touch event...

Scenario is like that...

->View ->ScrollView ->Textfield

I want to hide the keyboard on touch of scrollView. I tried to override the class for scrollview but still i can't do it.

like image 855
Ajay Sharma Avatar asked Sep 03 '10 04:09

Ajay Sharma


3 Answers

Doing like this will help:

@interface MyClass <UIScrollViewDelegate> {
}

@implementation

- (void)viewDidLoad {
  yourScrollView.delegate = self;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  [myTextField resignFirstResponder];
}

If you really want to handle the touch event, then you need to subclass the UIScrollView and override the method:

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {

}

More about UIScrollView touch

like image 166
vodkhang Avatar answered Oct 30 '22 13:10

vodkhang


This is what worked for me

in your viewController's viewDidLoad-method

    UITapGestureRecognizer *tapScroll = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapped)];
    tapScroll.cancelsTouchesInView = NO;
    [viewScroller addGestureRecognizer:tapScroll];

where viewScroller is your scroller. In the tapped-method we have

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

Don't know why but the above did not work for me...even though it should

like image 21
Groot Avatar answered Oct 30 '22 13:10

Groot


Try this:

Add gesturerecognizer for scrollview,

UITapGestureRecognizer *tapScroll = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)];
    tapScroll.cancelsTouchesInView = NO;
    [yourScrollview addGestureRecognizer:tapScroll];
    [tapScroll release];

Resign your keyboard in (tapped:) method.

like image 30
Dax Avatar answered Oct 30 '22 14:10

Dax