我一直在到处找这个,但我似乎找不到。我知道如何解雇使用Objective-C键盘,但我不知道如何使用Swift?有人知道吗?


当前回答

使用IQKeyboardmanager,将帮助您解决简单.....

/////////////////////////////////////////

[如何禁用键盘..][1].

import UIKit

class ViewController: UIViewController,UITextFieldDelegate {

   @IBOutlet weak var username: UITextField!
   @IBOutlet weak var password: UITextField!

   override func viewDidLoad() {
      super.viewDidLoad() 
      username.delegate = self
      password.delegate = self
      // Do any additional setup after loading the view, typically from a nib.
   }

   override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
   }

   func textFieldShouldReturn(textField: UITextField!) -> Bool // called when   'return' key pressed. return NO to ignore.
   {
      textField.resignFirstResponder()
      return true;
   }

   override func touchesBegan(_: Set<UITouch>, with: UIEvent?) {
     username.resignFirstResponder()
     password.resignFirstResponder()
     self.view.endEditing(true)
  }
}

其他回答

我发现最好的解决方案包括来自@Esqarrouth的接受答案,并进行了一些调整:

extension UIViewController {
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboardView")
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    }

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

电话响了。cancelsTouchesInView = false是关键的:它确保UITapGestureRecognizer不会阻止视图上的其他元素接收用户交互。

方法解雇键盘()被更改为稍微不那么优雅的解雇keyboardview()。这是因为在我的项目相当旧的代码库中,有很多次已经使用了dismissKeyboard()(我想这并不少见),导致编译器问题。

然后,如上所述,这个行为可以在各个视图控制器中启用:

override func viewDidLoad() {
    super.viewDidLoad()
    self.hideKeyboardWhenTappedAround() 
}

viewDidLoad()方法中只有一行代码:

view.addGestureRecognizer(UITapGestureRecognizer(target: view, action: #selector(UIView.endEditing(_:))))

当视图中有多个文本字段时

为了遵循@modocache的建议避免调用view. enditing(),您可以跟踪成为第一响应器的文本字段,但这很混乱且容易出错。

另一种方法是在视图控制器中的所有文本字段上调用resignFirstResponder()。下面是一个创建所有文本字段集合的示例(在我的情况下,验证代码无论如何都需要):

@IBOutlet weak var firstName: UITextField!
@IBOutlet weak var lastName: UITextField!
@IBOutlet weak var email: UITextField!

var allTextFields: Array<UITextField>!  // Forced unwrapping so it must be initialized in viewDidLoad
override func viewDidLoad()
{
    super.viewDidLoad()
    self.allTextFields = [self.firstName, self.lastName, self.email]
}

有了可用的集合,就可以简单地遍历所有这些集合:

private func dismissKeyboard()
{
    for textField in allTextFields
    {
        textField.resignFirstResponder()
    }
}

所以现在你可以在你的手势识别器(或任何适合你的地方)中调用遣散键盘()。缺点是在添加或删除字段时必须维护UITextFields列表。

欢迎评论。如果在不是第一响应者的控件上调用resignFirstResponder()有问题,或者如果有一种简单且保证无bug的方法来跟踪当前的第一响应者,我很乐意听到它!

对于Swift 3来说,这非常简单

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

如果你想隐藏键盘按回车键

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

但在第二种情况下,你还需要通过委托从所有文本字段的ViewController在主。故事板

使用IQKeyboardmanager,将帮助您解决简单.....

/////////////////////////////////////////

[如何禁用键盘..][1].

import UIKit

class ViewController: UIViewController,UITextFieldDelegate {

   @IBOutlet weak var username: UITextField!
   @IBOutlet weak var password: UITextField!

   override func viewDidLoad() {
      super.viewDidLoad() 
      username.delegate = self
      password.delegate = self
      // Do any additional setup after loading the view, typically from a nib.
   }

   override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
   }

   func textFieldShouldReturn(textField: UITextField!) -> Bool // called when   'return' key pressed. return NO to ignore.
   {
      textField.resignFirstResponder()
      return true;
   }

   override func touchesBegan(_: Set<UITouch>, with: UIEvent?) {
     username.resignFirstResponder()
     password.resignFirstResponder()
     self.view.endEditing(true)
  }
}