我一直在到处找这个,但我似乎找不到。我知道如何解雇使用Objective-C键盘,但我不知道如何使用Swift?有人知道吗?
当前回答
另一种可能是在你可能需要触摸的所有视图下面添加一个没有内容的大按钮。 给它一个名为:
@IBAction func dismissKeyboardButton(sender: AnyObject) {
view.endEditing(true)
}
对我来说,手势识别器的问题是,它也捕捉了我想通过tableViewCells接收的所有触摸。
其他回答
您还可以添加一个点击手势识别器来重新使用键盘。: D
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let recognizer = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
backgroundView.addGestureRecognizer(recognizer)
}
func handleTap(recognizer: UITapGestureRecognizer) {
textField.resignFirstResponder()
textFieldtwo.resignFirstResponder()
textFieldthree.resignFirstResponder()
println("tappped")
}
当视图中有多个文本字段时
为了遵循@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的方法来跟踪当前的第一响应者,我很乐意听到它!
我没有看到它被提到过,但是NotificationCenter可以告诉你键盘何时打开和关闭。例如,如果你想改变cancelsTouchesInView或一起删除手势识别器,这可以被使用。
例子:
var dismissKeyboardGestureRecognizer = UITapGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
//Setup dismiss keyboard gesture
self.dismissKeyboardGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
//Add Notifications
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name:UIResponder.keyboardDidShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidHide), name:UIResponder.keyboardDidHideNotification, object: nil)
}
@objc func keyboardDidShow() {
dismissKeyboardGestureRecognizer.cancelsTouchesInView = true
view.addGestureRecognizer(dismissKeyboardGestureRecognizer)
}
@objc func keyboardDidHide() {
dismissKeyboardGestureRecognizer.cancelsTouchesInView = false
view.removeGestureRecognizer(dismissKeyboardGestureRecognizer)
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
使用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)
}
}
我得到了你
override func viewDidLoad() {
super.viewDidLoad() /*This ensures that our view loaded*/
self.textField.delegate = self /*we select our text field that we want*/
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: Selector("dismissKeyboard")))
}
func dismissKeyboard(){ /*this is a void function*/
textField.resignFirstResponder() /*This will dismiss our keyboard on tap*/
}
推荐文章
- Objective-C中方法混合的危险是什么?
- 如何使用接口生成器创建的nib文件加载UIView
- iOS如何设置应用程序图标和启动图像
- 更改UITextField和UITextView光标/插入符颜色
- 如何使用@Binding变量实现自定义初始化
- 'Project Name'是通过优化编译的——步进可能会表现得很奇怪;变量可能不可用
- Swift设置为Array
- 如何设置回退按钮文本在Swift
- 我如何能在Swift扩展类型化数组?
- Swift类错误:属性未在super处初始化。init调用
- 模拟器慢动作动画现在打开了吗?
- 如何为TableView创建NSIndexPath
- 滑动删除和“更多”按钮(就像iOS 7的邮件应用程序)
- 使UINavigationBar透明
- 如何改变推和弹出动画在一个基于导航的应用程序