簡體   English   中英

敲擊UITextField時如何停止顯示鍵盤?

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

// Xcode 8.3.3 | 迅捷3

免責聲明:好的,所以有一些關於SO的問答可以回答這個問題。 但是,經過大量的Google搜索,我無法在Swift中找到任何最新的信息(它們都在Objective C中,並且可以追溯到2010年,等等)。

我有一個項目,在該項目中,我需要用戶能夠點按UITextField並出現閃爍的線條,但是我已經使用UIButtons實現了數字鍵盤。 這意味着我不希望在按下文本字段時出現默認鍵盤或任何鍵盤。

情節提要ViewController

目前最簡單的方法是什么?

你可以這樣-

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

這不會打開鍵盤,但會允許光標閃爍。

嘗試這樣做:

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()
    }
}

@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