简体   繁体   中英

Best way to dismiss keyboard when tapping outside of UITextField - IOS

I've found a few threads here about this, and some videos online about it as well, but every solution seems to have problems reported by others. The simplest solution I've found is the one below.

    import UIKit

class SignupController: UIViewController, UITextFieldDelegate {

// Outlets
@IBOutlet weak var logoImage: UIImageView!
@IBOutlet weak var nameTF: CustomTextField!
@IBOutlet weak var emailTF: CustomTextField!
@IBOutlet weak var passwordTF: CustomTextField!
@IBOutlet weak var confirmPassTF: CustomTextField!

// Actions
@IBAction func signupButton(_ sender: UIButton) {
}


override func viewDidLoad() {
    super.viewDidLoad()
    logoImage.image = UIImage(named: "logo2")
    nameTF.delegate = self
    emailTF.delegate = self
    passwordTF.delegate = self
    confirmPassTF.delegate = self

}

// Moves to next text field each time return key is pressed
func textFieldShouldReturn(_ textField: UITextField) -> Bool {

    if textField == nameTF {
        textField.resignFirstResponder()
        emailTF.becomeFirstResponder()
    } else if textField == emailTF {
        textField.resignFirstResponder()
        passwordTF.becomeFirstResponder()
    } else if textField == passwordTF {
        textField.resignFirstResponder()
        confirmPassTF.becomeFirstResponder()
    }else if textField == confirmPassTF {
        textField.resignFirstResponder()
    }
    return true
}



// Dismisses keyboard when tapped
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

}

It works, is very simple, but my project and coding experience are in their infancy, so I'm not sure if this is the best method simply because it's short, or if there's something I'm missing due to lack of experience/knowledge?

Anybody know of a better solution, or is this one just fine?

Then you need to implement the gesture recognition for this . Or you can do like this :

override func viewDidLoad() {
    super.viewDidLoad()

    //Looks for single or multiple taps. 
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dissMissKeyboard))

    //Uncomment the line below if you want the tap not not interfere and cancel other interactions.
    //tap.cancelsTouchesInView = false 

    view.addGestureRecognizer(tap)
}

//Calls this function when the tap is recognized.

    func dissMissKeyboard() {
        //Causes the view (or one of its embedded text fields) to resign the first responder status.
        view.endEditing(true)
    }

I prefer to use UITextField delegate method:

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

or setup inputAccessoryView which have 'done' or 'exit' button.

just do this:

class viewController: UIViewController, UITextFieldDelegate {

// Outlets
@IBOutlet weak var logoImage: UIImageView!
@IBOutlet weak var nameTF: CustomTextField!
@IBOutlet weak var emailTF: CustomTextField!
@IBOutlet weak var passwordTF: CustomTextField!
@IBOutlet weak var confirmPassTF: CustomTextField!

// Actions
@IBAction func signupButton(_ sender: UIButton) {
}


override func viewDidLoad() {
    super.viewDidLoad()
    logoImage.image = UIImage(named: "logo2")
    nameTF.delegate = self
    emailTF.delegate = self
    passwordTF.delegate = self
    confirmPassTF.delegate = self
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dissMissKeyboard))
    view.addGestureRecognizer(tap)

}
func dissMissKeyboard() {
    view.endEditing(true)
}

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