简体   繁体   中英

Hide keyboard automatically in swift

Is there a way to automatically hide keyboard in swift after inputting four characters? I actually have a code that hides the keyboard but the user has to click anywhere on the screen. Here's the code:

 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    self.view.endEditing(true)
}

Thanks in advance!

If I got your question correctly, Consider below example code:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var txtF: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        txtF.delegate = self
        txtF.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
    }


    func textFieldDidChange(textField: UITextField) {

        if textField == txtF {

            if textField.text?.characters.count == 4 {

                self.txtF.resignFirstResponder()
            }
        }
    }

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

        if textField == txtF {

            if textField.text?.characters.count > 3 {

                self.txtF.resignFirstResponder()
                return false
            } else {

                return true
            }
        } else {

            return true
        }
    }
}

With above code keyboard will hide when textField have 4 characters and after that if user again tap on textField keyboard will pop up be user will not able to enter any text into textField and keyboard will hide again.

Result will be:

在此处输入图片说明

Hope this will help.

Here is the simplest way to hide the keyboard or a numberpad. First you need a button and you need to make it the size of the screen.Send it to the back of the scene and connect it to an IBAction.Then you code should look like this:

@IBAction func HideKeyboard(sender: AnyObject) {
   YourKeyboardHere.resignFirstResponder()
}

This should work for all types of keyboard.

This worked for me:

// Outlet to textfield, editing changed
@IBAction func textFieldEditingChanged(sender: UITextField) {
    if sender.text?.characters.count == 4 {
        view.endEditing(true)
    }
    // Optional if you don't want the user to paste in more than 4 characters
    else if sender.text?.characters.count > 4   {
        sender.text = nil
    }
}

I also recommend you setting the

textField.clearsOnBeginEditing = true 

so the textField gets cleared when the user clicks on it again.

You should use textfield delegate shouldChangeCharactersInRange something like,

 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{


if (textField.text.length == 4) {

    [textField resignFirstResponder];
}

return YES;
}

In Swift,

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {



        if textField.text?.characters.count == 4 {

            textField.resignFirstResponder()

        } 
    return true
}

Hope this will help :)

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