Dismissing the keyboard in a UIScrollView

后端 未结 12 2248
醉话见心
醉话见心 2020-11-30 20:18

Alright, I have a couple of UITextFields and UITextViews inside a UIScrollView, and I\'d like to set the keyboard to disappear wheneve

相关标签:
12条回答
  • 2020-11-30 20:40

    Try This

    [self.selectedViewController.view endEditing:YES];

    0 讨论(0)
  • 2020-11-30 20:45

    Here is the cleanest way to achieve this in iOS 7.0 and above.

    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    

    Or

    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
    

    In Swift:

    scrollView.keyboardDismissMode = .onDrag
    

    Or

    scrollView.keyboardDismissMode = .interactive
    
    0 讨论(0)
  • 2020-11-30 20:45

    Bit late but if anyone else is searching an answer to this problem, this is how I have gone about solving it:

    1) Create a tap gesture recognizer with a target callback method to dismiss your keyboard using resignFirstResponder on all your fields.

    2) Add the tap gesture to the scrollview.

    Here's an example:

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    
    // prevents the scroll view from swallowing up the touch event of child buttons
    tapGesture.cancelsTouchesInView = NO;    
    
    [pageScrollView addGestureRecognizer:tapGesture];
    
    [tapGesture release];
    
    ...
    
    // method to hide keyboard when user taps on a scrollview
    -(void)hideKeyboard
    {
        [myTextFieldInScrollView resignFirstResponder];
    }
    
    0 讨论(0)
  • 2020-11-30 20:48

    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
    }
    
    0 讨论(0)
  • 2020-11-30 20:49

    Although the essence is the same, I prefer less code.

    Setting the keyboard to disappear when the scrollView is scrolled in Attributes inspector:

    make keyboard disappear when scrollView is scrolled

    Then disappear keyboard when scrollView is tapped:

    1. Drag a Tap Gesture Recognizer onto your scrollView
    2. Ctrl-Drag
    3. Make an action
    4. Only one line in the action —— scrollView.endEditing(true). If you are using Objective-C, [self.scrollView endEditing: YES];
    0 讨论(0)
  • 2020-11-30 20:49
        extension UIView{
        //Set tag via storyboard 
        func keyboardDissmissInteractiveMode(_ tag:Int){
            if let scrollView = self.viewWithTag(tag) as? UIScrollView{
                scrollView.keyboardDismissMode = .interactive
            }
            if let tableview = self.viewWithTag(tag) as? UITableView{
                tableview.keyboardDismissMode = .interactive
            }
        }
    
        func keyboardDissmissOnDragMode(_ tag:Int){
            if let scrollView = self.viewWithTag(tag) as? UIScrollView{
                scrollView.keyboardDismissMode = .onDrag
            }
            if let tableview = self.viewWithTag(tag) as? UITableView{
                tableview.keyboardDismissMode = .onDrag
            }
        }
    
    
        func keyboardDissmissInteractiveMode(_ view:UIView){
            if let scrollView = view as? UIScrollView{
                scrollView.keyboardDismissMode = .interactive
            }
            if let tableview = view as? UITableView{
                tableview.keyboardDismissMode = .interactive
            }
        }
    
        func keyboardDissmissOnDragMode(_ view:UIView){
            if let scrollView = view as? UIScrollView{
                scrollView.keyboardDismissMode = .onDrag
            }
            if let tableview = view as? UITableView{
                tableview.keyboardDismissMode = .onDrag
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题