xcode Swift - 如何在文本字段之外点击后关闭数字键盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27878732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:28:29  来源:igfitidea点击:

Swift - How to dismiss number keyboard after tapping outside of the textfield

iosxcodeswift

提问by momentsnapper

How would you dismiss the keyboard from the UITextField when tapping outside of the keyboard. I have tried resignFirstResponder()and it only exits after typing one number. I have also tried textField.inputView = UIView.frame(frame: CGRectZero). I have seen many Obj-C verisons of what I'm asking but I need the Swift equivalent because I have no programming experience in Objective-C

在键盘外部轻敲时,您将如何从 UITextField 关闭键盘。我试过了resignFirstResponder(),它只在输入一个数字后才退出。我也试过了textField.inputView = UIView.frame(frame: CGRectZero)。我已经看到了很多我要问的 Obj-C 版本,但我需要 Swift 等价物,因为我没有 Objective-C 的编程经验

Thank you for your time and patience.

感谢您的时间和耐心。

采纳答案by alex

You could also use this method to dismiss the keyboard when pressing 'Return'

您也可以使用此方法在按“返回”时关闭键盘

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

Make sure to set your delegate

确保设置您的委托

回答by momentsnapper

The best way to add a tap gesture recognizer to the view and calling either resignFirstResponder() or self.view.endEditing(true). I prefer endEditing() since resignFirstResponder has to be done for each text field separately unlike endEditing which is done for the view itself.

将点击手势识别器添加到视图并调用 resignFirstResponder() 或 self.view.endEditing(true) 的最佳方法。我更喜欢 endEditing() 因为 resignFirstResponder 必须分别为每个文本字段完成,不像 endEditing 是为视图本身完成的。

In viewDidLoad, write the below code:

在 viewDidLoad 中,编写以下代码:

let tapRecognizer = UITapGestureRecognizer()
tapRecognizer.addTarget(self, action: "didTapView")
self.view.addGestureRecognizer(tapRecognizer)

Now write the didTapView method to dismiss the keyboard:

现在编写 didTapView 方法来关闭键盘:

func didTapView(){
  self.view.endEditing(true)
}

Now when you tap outside the keyboard on the main view of the controller, it will call the didTapView method and dismiss the keyboard.

现在,当您在控制器的主视图上点击键盘外部时,它将调用 didTapView 方法并关闭键盘。

Swift 3.x

斯威夫特 3.x

The code in viewDidLoad should be:

viewDidLoad 中的代码应该是:

let tapRecognizer = UITapGestureRecognizer()
tapRecognizer.addTarget(self, action: #selector(ViewController.didTapView))
self.view.addGestureRecognizer(tapRecognizer)

where ViewController should be the name of your view controller.

其中 ViewController 应该是您的视图控制器的名称。

Thanks

谢谢

回答by stan

Swift 3 tested and working

Swift 3 测试和工作

 // dismiss keyboard on touch outside textfield
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for txt in self.view.subviews {
            if txt.isKind(of: UITextField.self) && txt.isFirstResponder {
                txt.resignFirstResponder()
            }
        }
    }   

Enjoy

享受

Swift 2.3 tested and working

Swift 2.3 测试和工作

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for txt in self.view.subviews {
            if txt.isKindOfClass(UITextField.self) && txt.isFirstResponder() {
                txt.resignFirstResponder()
            }
        }
    }

Enjoy

享受

回答by Jawwad

If you don't want to define an extra method, there is a slightly simpler way that will also work

如果您不想定义额外的方法,还有一种稍微简单的方法也可以使用

let tapRecognizer = UITapGestureRecognizer(target: self, action: "endEditing:")
view.addGestureRecognizer(tapRecognizer)