简体   繁体   English

敲击UITextField时如何停止显示键盘?

[英]How to Stop Keyboard From Showing When UITextField is Tapped?

//Xcode 8.3.3 | // Xcode 8.3.3 | Swift 3 迅捷3

Disclaimer: Alright, so there are some Q&A on SO that answer this question. 免责声明:好的,所以有一些关于SO的问答可以回答这个问题。 However, after extensive Googling, I was not able to find any that are up-to date in Swift (they're all in Objective C and date back to 2010, etc). 但是,经过大量的Google搜索,我无法在Swift中找到任何最新的信息(它们都在Objective C中,并且可以追溯到2010年,等等)。

I have a project in which I need the user to be able to tap a UITextField and for the blinking line to come up, but I already have a number pad implemented with UIButtons. 我有一个项目,在该项目中,我需要用户能够点按UITextField并出现闪烁的线条,但是我已经使用UIButtons实现了数字键盘。 This means that I do not want the default or any keyboard to come up when the text field is pressed. 这意味着我不希望在按下文本字段时出现默认键盘或任何键盘。

情节提要ViewController

What is the easiest way to achieve this currently? 目前最简单的方法是什么?

You can do like this - 你可以这样-

YourTextView.inputView = UIView()
YourTextView.inputAccessoryView = UIView() 

this will not open the keyboard but allow cursor blink. 这不会打开键盘,但会允许光标闪烁。

try do this: 尝试这样做:

extension YourController: UITextFieldDelegate {

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        // do some things

        NotificationCenter.default.addObserver(self, selector: #selector(keyboardHidden), 
        name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        return false
    }

    @objc private func keyboardHidden() {
        // disabled keyboard 
        yourTextField.resignFirstResponder()
    }
}

The answer linked by @paulvs translated to Swift 3 would be: @paulvs链接到Swift 3的答案是:

let tempView = UIView(frame: .zero)
yourTextField.inputView = tempView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM