简体   繁体   English

快速自动隐藏键盘

[英]Hide keyboard automatically in swift

Is there a way to automatically hide keyboard in swift after inputting four characters?输入四个字符后,有没有办法在swift中自动隐藏键盘? 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.使用上面的代码,当 textField 有 4 个字符时键盘将隐藏,之后如果用户再次点击 textField 键盘将弹出用户将无法在 textField 中输入任何文本并且键盘将再次隐藏。

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。然后你的代码应该是这样的:

@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.所以当用户再次点击 textField 时,它会被清除。

You should use textfield delegate shouldChangeCharactersInRange something like,您应该使用文本字段委托shouldChangeCharactersInRange类的,

 -(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 :)希望这会有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM