hide keyboard for text field in swift programming language

后端 未结 14 1107
不思量自难忘°
不思量自难忘° 2020-11-27 17:07

I have little experience in Objective-C. I want to hide the keyboard for a text field using the Swift programming language.

I also tried this

func te         


        
14条回答
  •  不知归路
    2020-11-27 17:35

    • Add UITextFieldDelegate to the class declaration:

      class ViewController: UIViewController, UITextFieldDelegate

    • Connect the textfield or write it programmatically

      @IBOutlet weak var userText: UITextField!

    • set your view controller as the text fields delegate in view did load:

      override func viewDidLoad() {
      super.viewDidLoad()
      self.userText.delegate = self
      }
      
    • Add the following function

      func textFieldShouldReturn(userText: UITextField!) -> Bool {
          userText.resignFirstResponder()
          return true;
      }
      

      with all this your keyboard will begin to dismiss by touching outside the textfield aswell as by pressing return key.

提交回复
热议问题