hide keyboard for text field in swift programming language

后端 未结 14 1070
不思量自难忘°
不思量自难忘° 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:34

    Now In Swift 3/4/5, the easiest methods are

    Method 1: called when 'return' key pressed.

    func textFieldShouldReturn(_ textField: UITextField) -> Bool 
     {
     textField1.resignFirstResponder()
     textField2.resignFirstResponder()
            return true;
        }
    

    Method 2: called when 'return' key pressed.

    func textFieldShouldReturn(_ textField: UITextField) -> Bool 
        {
        self.view.endEditing(true)
            return true;
        }
    

    Method 3: Global Method Write in view did load

    self.view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))
    

    Note: Don't forget to add this. Other wise its not work.

    UIGestureRecognizerDelegate, UITextFieldDelegate
    

    I hope it will work for you :)

提交回复
热议问题