Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在屏幕上轻敲键盘_Ios_Swift_Keyboard - Fatal编程技术网

Ios 如何在屏幕上轻敲键盘

Ios 如何在屏幕上轻敲键盘,ios,swift,keyboard,Ios,Swift,Keyboard,我正在寻找一个简单的解决方案在swift中添加的能力,点击屏幕上的任何地方,关闭键盘。我在这里读了很多答案,但他们都给了我错误。我以前用过的是: override func viewDidLoad() { super.viewDidLoad() let tap: UITapGestureRecognizer = UITapGestureRecognizer(target:self, action: #selector(ViewController.dismissKeyboard

我正在寻找一个简单的解决方案在swift中添加的能力,点击屏幕上的任何地方,关闭键盘。我在这里读了很多答案,但他们都给了我错误。我以前用过的是:

override func viewDidLoad() {
    super.viewDidLoad()


   let tap: UITapGestureRecognizer = UITapGestureRecognizer(target:self, action: #selector(ViewController.dismissKeyboard))

    view.addGestureRecognizer(tap)

这对我的一个项目有效,但似乎对其他项目无效。每当我尝试将此添加到其他项目时,我都会收到错误类型“ViewController”没有成员“dismissKeyboard”。

您需要在对它的任何引用之上添加一个方法。我将此代码放在文件的开头:

   func dismissKeyboard() {
    //Causes the view (or one of its embedded text fields) to resign the first responder status and drop into background
    view.endEditing(true)
   }
然后,每当我需要引用.dismiss键盘时,我都会在viewDidLoad中使用它:

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(LoginViewController.dismissKeyboard))
    view.addGestureRecognizer(tap)  // Allows dismissal of keyboard on tap anywhere on screen besides the keyboard itself
并确保将“LoginViewController”替换为当前视图控制器。根据您的示例,这只是“ViewController”

如果您正在寻找更深入的答案,请参见7KV7:

您需要在对它的任何引用之上添加一个方法。我将此代码放在文件的开头:

   func dismissKeyboard() {
    //Causes the view (or one of its embedded text fields) to resign the first responder status and drop into background
    view.endEditing(true)
   }
然后,每当我需要引用.dismiss键盘时,我都会在viewDidLoad中使用它:

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(LoginViewController.dismissKeyboard))
    view.addGestureRecognizer(tap)  // Allows dismissal of keyboard on tap anywhere on screen besides the keyboard itself
并确保将“LoginViewController”替换为当前视图控制器。根据您的示例,这只是“ViewController”

如果您正在寻找更深入的答案,请参见7KV7:

这很好用,试试看


这很好,试试看。

你可以试试这一个,它非常简单的解决方案,在swift中经常被用来关闭键盘

只需添加这个函数,就可以了

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

你可以试试这个,它是一个非常简单的解决方案,在swift中经常被用来关闭键盘

只需添加这个函数,就可以了

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

你能同时显示你的解雇键盘方法代码吗?你能同时显示你的解雇键盘方法代码吗?