Resign First Responder on ScrollView Touch

一笑奈何 提交于 2019-12-12 08:03:45

问题


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.


回答1:


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




回答2:


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




回答3:


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.




回答4:


Please, take a look at this answer, This is the easiest one I found.

UitextField resignFirstResponder does not work in scroll view




回答5:


- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
    [self.view endEditing:YES];
     return YES;
 }


来源:https://stackoverflow.com/questions/3633055/resign-first-responder-on-scrollview-touch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!