62

好吧,我有几个UITextFieldsandUITextViews在 aUIScrollView中,我想将键盘设置为在scrollview触摸或滚动时消失(当然,除非您在文本字段/视图内触摸时)。

我目前这样做的尝试是用子类替换,并将其设置为在touchesBegan方法UIScrollView内调用 removeKeyboard 函数(在主视图控制器内定义) 。但是,这只会在正常触摸时移除键盘,而不是在简单地滚动视图时。那么,什么是移除键盘内部的最佳方法?UIScrollView

在此先感谢您的帮助。

4

12 回答 12

143

这是在 iOS 7.0 及更高版本中实现此目的的最简洁方法。

scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

或者

scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;

在斯威夫特:

scrollView.keyboardDismissMode = .onDrag

或者

scrollView.keyboardDismissMode = .interactive
于 2013-11-18T03:11:56.027 回答
51

有点晚了,但如果其他人正在寻找这个问题的答案,这就是我解决它的方法:

1) 创建一个带有目标回调方法的点击手势识别器,以在所有字段上使用 resignFirstResponder 关闭键盘。

2)将点击手势添加到滚动视图。

这是一个例子:

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];
}
于 2012-06-22T03:11:05.493 回答
39

虽然本质是一样的,但我更喜欢少一些代码。

在属性检查器中滚动scrollView时将键盘设置为消失:

滚动scrollView时使键盘消失

然后在点击 scrollView 时消失键盘:

  1. 将 Tap Gesture Recognizer 拖到您的滚动视图上
  2. Ctrl-拖动
  3. 做一个动作
  4. 动作中只有一行—— scrollView.endEditing(true)。如果你使用的是 Objective-C,[self.scrollView endEditing: YES];
于 2015-03-25T15:29:20.617 回答
10

斯威夫特

有点晚了,但如果其他人正在寻找这个问题的答案,这就是我解决它的方法:

1) 创建一个带有目标回调方法的点击手势识别器,以在所有字段上使用 resignFirstResponder 关闭键盘。

2)将点击手势添加到滚动视图。

这是一个例子:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var t1: UITextField!
    @IBOutlet var t2: UITextField!
    @IBOutlet var t3: UITextField!
    @IBOutlet var t4: UITextField!

    @IBOutlet var srcScrollView: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "hideKeyboard")

        // prevents the scroll view from swallowing up the touch event of child buttons
        tapGesture.cancelsTouchesInView = false

        srcScrollView.addGestureRecognizer(tapGesture)
    }

    func hideKeyboard() {
        t1.resignFirstResponder()
        t2.resignFirstResponder()
        t3.resignFirstResponder()
        t4.resignFirstResponder()
    }
}
于 2014-12-06T15:37:07.977 回答
9

查看 UIScrollView 的keyboardDismissMode属性。

// will hide keyboard when your text field is about to go beyond the keyboard.
vwScrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;

// will hide keyboard instantly once the scroll view started scrolling by user.
vwScrollView.keyboardDismissMode = UIScrollViewKeyboardDismissOnDrag;

// If you need to hide keyboard on tap of scroll view,consider adding a tap gesture or sub class and override touchesbegan: method.

斯威夫特版本

vwScrollView.keyboardDismissMode = .interactive
vwScrollView.keyboardDismissMode = .onDrag
于 2016-09-23T15:02:45.580 回答
4

尝试这个

[self.selectedViewController.view endEditing:YES];
于 2013-07-22T11:02:04.740 回答
4

创建一个扩展类以在任何地方触摸滚动视图/视图时隐藏键盘

extension UIViewController {
  func hideKeyboardWhenTappedAround() {
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
    tap.cancelsTouchesInView = false
    view.addGestureRecognizer(tap)
  }
    
  @objc func dismissKeyboard() {
    view.endEditing(true)
  }
}

并在 viewDidLoad 中调用此方法,如

override func viewDidLoad() {
  super.viewDidLoad()
  self.hideKeyboardWhenTappedAround()    
}
于 2018-05-14T05:41:35.543 回答
3

有点晚了,但如果其他人正在用Swift 3搜索这个问题的答案:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    view.endEditing(true)
}
于 2016-09-23T14:28:40.873 回答
2

当我将手势添加到 的子类时UIScrollView,我遇到了视图树中的各种手势相互干扰的问题,例如能够单击子视图、滚动视图以及在所有情况下都关闭键盘。我想出了这个解决方案,它可以从超类UIScrollViewUIViewController.

该类DismissKeyboardTapGesture使用 ARC,适用于视图下的任何文本字段,并且不会接管来自按钮等子视图的任何点击。还利用 iOS7 滚动效果来关闭键盘。

从 UISScrollView 超类设置:

    _dismissKeyboard = [[DismissKeyboardTapGesture alloc] initWithView:self];

或来自 UIViewController:

    _dismissKeyboard = [[DismissKeyboardTapGesture alloc] initWithView:self.view];

这是课程:

@interface DismissKeyboardTapGesture : NSObject <UIGestureRecognizerDelegate>

@end

@implementation DismissKeyboardTapGesture

- (id)initWithView:(UIView *)view
{
    self = [super init];
    if (self) {
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
        singleTap.cancelsTouchesInView = NO;
        singleTap.delegate = self;
        [view addGestureRecognizer:singleTap];

        if ([view respondsToSelector:@selector(setKeyboardDismissMode:)]) {
            // Bonus effect to dismiss keyboard by scrolling
            ((UIScrollView *)view).keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
        }
    }
    return self;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    // Don't stop any existing gestures in our view from working
    if (otherGestureRecognizer.view == gestureRecognizer.view) {
        return YES;
    }
    return NO;
}

- (void)singleTap:(UIGestureRecognizer*)gestureRecognizer
{
    // Close keyboard for any text edit views that are children of the main view
    [gestureRecognizer.view endEditing:YES];
}

@end
于 2013-11-01T20:01:50.923 回答
1

试试这个滚动视图委托方法 -

链接 IB 中的委托以滚动视图,然后复制此代码(根据您的需要进行修改)。

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

这应该有效。您也可以尝试其他委托方法

   -(void)scrollViewDidScroll: (UIScrollView *)scrollView 
{
//do your stuff
}
于 2012-09-11T05:21:06.627 回答
1
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
于 2017-10-16T07:23:29.820 回答
1
    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
        }
    }
}
于 2019-07-05T15:09:55.127 回答