Dismissing the keyboard in a UIScrollView


Alright, I have a couple of UITextFields and UITextViews inside a UIScrollView, and I'd like to set the keyboard to disappear whenever the scrollview is touched or scrolled (except when you touch down inside the text field/view, of course).

My current attempt at doing this is replacing the UIScrollView with a subclass, and setting it to call a removeKeyboard function (defined inside the main view controller) inside the touchesBegan method. However, this only removes the keyboard for a normal touch, not when the view is simply scrolled. So, what's the best way to remove the keyboard inside a UIScrollView?

Thanks in advance for your help.


Try this scroll view delegate method -

link delegate in IB to scroll view and then cop this code (modify as per your need).

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
//sample code    
    [challengeABallotComponent.voterNameTextField resignFirstResponder];
    [challengeABallotComponent.ballotNumberTextField resignFirstResponder];
    [locationInformation.pollingLocation resignFirstResponder];

This should work. You can try other delegate methods too like

   -(void)scrollViewDidScroll: (UIScrollView *)scrollView 
//do your stuff

This video can help you solving your question :)
By: admin