Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hide keyboard on touch of scroll view

I have developed my app, after which, I discovered I need to add a Scroll view and embed all elements in it. After doing so, I noticed that the keyboard doesn't disappear on touch outside of the keyboard. I know because the IBAction is tied to the ViewController and not the scroller. the scroller can't have any IBAction though. Is there way you can help with this? I have searched a lot and kinda lost in hiding the keyboard simply after adding the Scroll view. This one shouldn't be that complicated.

Thanks!

like image 676
Joe Saad Avatar asked Feb 07 '13 01:02

Joe Saad


People also ask

How to dismiss keyboard on scroll in Swift?

Use . immediately to make the keyboard dismiss fully as soon as any scroll happens. Use . interactively to make the keyboard dismiss inline with the user's gesture – they need to scroll further to make it dismiss fully.

How do I hide the keyboard in react native?

Regular Views. If you have something other than a ScrollView and you'd like any presses to dismiss the keyboard, you can use a simple TouchableWithoutFeedback and have the onPress use React Native's utility library dismissKeyboard to dismiss the keyboard for you.

How do I turn off the keyboard on SwiftUI?

Pure SwiftUI (iOS 15) To dismiss the keyboard, simply set view's focusedField to nil . The return key will dismiss keyboard automatically (since iOS 14).


2 Answers

Cleanest way in iOS 7:

scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;

Swift version:

scrollView.keyboardDismissMode = .interactive
like image 64
Pei Avatar answered Oct 04 '22 21:10

Pei


Please try this:

UITapGestureRecognizer *yourTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollTap:)];
    [scrollView addGestureRecognizer:yourTap];
    [self.view addSubview:scrollView];

Then in

- (void)scrollTap:(UIGestureRecognizer*)gestureRecognizer {

    //make keyboard disappear , you can use resignFirstResponder too, it depends.
    [self.view endEditing:YES];
}

*** remarkable you said you had to embed all elements in to scrollview right?, that means you have added something like UIButton as a subview to scrollview and then when you init UIButton you should add action too, so in scrollTap just [self.view endEditting:YES] is enough cuz if you press at some point in UIScrollView and if that point is UIButton, it will activate action of UIButton cuz it stays in the most upper layer as you add it as a subview and if that point is not a button so scrollTap will be activated. Sorry I didn't look to it carefully but it should be like I said.

like image 43
piam Avatar answered Oct 04 '22 22:10

piam