在UIScrollView中关闭键盘

Nic*_*024 59 keyboard objective-c uiscrollview ios swift

好吧,我有几个UITextFields并且UITextViews在里面UIScrollView,我想设置键盘在scrollview触摸或滚动时消失(当然,当你在文本字段/视图内部触地时).

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

在此先感谢您的帮助.

Pei*_*Pei 133

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

scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
Run Code Online (Sandbox Code Playgroud)

要么

scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
Run Code Online (Sandbox Code Playgroud)

迅速:

scrollView.keyboardDismissMode = .onDrag
Run Code Online (Sandbox Code Playgroud)

要么

scrollView.keyboardDismissMode = .interactive
Run Code Online (Sandbox Code Playgroud)

  • OnDrag表示在开始拖动滚动视图时关闭键盘.而交互式意味着无论何时开始滚动视图中的触摸. (4认同)

Zha*_*ang 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];
}
Run Code Online (Sandbox Code Playgroud)


fuj*_*471 34

虽然本质是相同的,但我更喜欢更少的代码.

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

滚动scrollView时键盘会消失

然后点击scrollView时消失键盘:

  1. 将Tap Gesture Recognizer拖到滚动视图上
  2. 按Ctrl键
  3. 采取行动
  4. 行动中只有一行 - scrollView.endEditing(true).如果您使用的是Objective-C,[self.scrollView endEditing: YES];


Kin*_*ard 10

Swift中:

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

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()
    }
}
Run Code Online (Sandbox Code Playgroud)


Nar*_*y M 7

查看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.
Run Code Online (Sandbox Code Playgroud)

Swift版本

vwScrollView.keyboardDismissMode = .interactive
vwScrollView.keyboardDismissMode = .onDrag
Run Code Online (Sandbox Code Playgroud)