2

I have read some other articles like here and here but unfortunately there are some differences in my code that won't make these work (so please don't suggest these answers).

I would like to dismiss the keyboard when the user taps the background. Normally this would be easy, except that my UITextField objects are inside a UIScrollView which makes it so I can't catch the touch events (the UIScrollView swallows them so they don't reach the base view). One way to get around this is to register for a generic gesture (a tap), but this catches all taps, including the ones intended for the submit button.

So basically, 'touchesBegan:withEvent:' wont work because it never gets called, and gestures wont work because they dont account for button presses.

Here's the question: is there some way to detect a simple tap on a UIScrollView? Once I detect the tap I know how to do the rest. Thanks!

Community
  • 1
  • 1
mtmurdock
  • 12,756
  • 21
  • 65
  • 108

2 Answers2

2

You can't use touchesBegan:withEvent on the superview of the scrollview, but what about subclassing UIScrollView and handling the touch there? You can then proceed normally with a call to super's implementation to keep from stepping on the UIScrollView's toes:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    /* Insert code to dismiss keyboard if needed */

    // This makes sure scrolling proceeds normally.
    [super touchesBegain:touches withEvent:event];
}
Matt Wilding
  • 20,115
  • 3
  • 67
  • 95
  • So, if the user only tapped the screen will `touches` have a size of 1? Also, will this interfere with buttons in the ScrollView? – mtmurdock May 16 '11 at 21:07
  • The general theory is that the event will be called each time a finger touches the screen. Since it is theoretically possible that two fingers touch the screen at _exactly_ the same time, the touches are passed as a set. Practically speaking, touches almost always has one touch, and the messages are sent with very close temporal proximity for touches with many fingers. As for the buttons, they will have a higher priority in the responder chain, and I believe they will consume their own touches without the scroll view knowing anything. – Matt Wilding May 16 '11 at 21:12
  • You've been great, thanks. So do you have any ideas on how to differentiate between a touch and a scroll? – mtmurdock May 16 '11 at 21:18
  • @mtmurdock: For the purpose of not dismissing the keyboard when the user is actually trying to scroll? – Matt Wilding May 16 '11 at 21:27
  • Yea basically. I actually implemented it simply checking if `[touches count] == 1` and that seems to work just fine. Thank you for your help! – mtmurdock May 18 '11 at 18:22
0

I also ran into same problem. While in IOS 6.0, it was working fine, but as soon i switched to IOS 5.0, it started to show the same behaviour you have mentioned.

Workout for IOS 5.0 - You should use the UITapGestureRecognizer. Then set its delegate to self and implement the following delegate method

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;

as

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return ! ([touch.view isKindOfClass:[UIControl class]]);
}
@end

above code will verify that the object under touch is not a UIButton or any control element, then only handle the touch

I hope it would solve your problem.

ajonnet
  • 215
  • 2
  • 9
  • [link] (http://www.techotopia.com/index.php/Writing_iOS_4_Code_to_Hide_the_iPhone_Keyboard) here is another strategy to handle the same – ajonnet Mar 01 '13 at 08:10