简体   繁体   中英

How to Stop Keyboard From Showing When UITextField is Tapped?

//Xcode 8.3.3 | Swift 3

Disclaimer: Alright, so there are some Q&A on SO that answer this question. 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).

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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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