使用Swift触摸任意位置以关闭iOS键盘

本文翻译自:Close iOS Keyboard by touching anywhere using Swift

I have been looking all over for this but I can't seem to find it. 我一直在寻找这个,但似乎找不到。 I know how to dismiss the keyboard using Objective-C but I have no idea how to do that using Swift ? 我知道如何使用Objective-C消除键盘,但我不知道如何使用Swift做到这一点? Does anyone know? 有人知道吗?


#1楼

参考:https://stackoom.com/question/1dERy/使用Swift触摸任意位置以关闭iOS键盘


#2楼

You can call 你可以打电话

resignFirstResponder()

on any instance of a UIResponder, such as a UITextField. 在UIResponder的任何实例(例如UITextField)上。 If you call it on the view that is currently causing the keyboard to be displayed then the keyboard will dismiss. 如果在当前导致键盘显示的视图上调用它,则键盘将关闭。


#3楼

Dash's answer is correct and preferred. 达世币的答案是正确的并且是首选。 A more "scorched earth" approach is to call view.endEditing(true) . 一种更“焦土”的方法是调用view.endEditing(true) This causes view and all its subviews to resignFirstResponder . 这将导致view及其所有子视图为resignFirstResponder If you don't have a reference to the view you'd like to dismiss, this is a hacky but effective solution. 如果您没有要关闭的视图的引用,那么这是一种骇人但有效的解决方案。

Note that personally I think you should have a reference to the view you'd like to have resign first responder. 请注意,我个人认为您应该参考要辞职的第一视图。 .endEditing(force: Bool) is a barbaric approach; .endEditing(force: Bool)是一种野蛮的方法; please don't use it. 请不要使用它。


#4楼

An answer to your question on how to dismiss the keyboard in Xcode 6.1 using Swift below: 有关以下有关如何使用Swift在Xcode 6.1中关闭键盘的问题的答案:

import UIKit

class ItemViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet var textFieldItemName: UITextField!

    @IBOutlet var textFieldQt: UITextField!

    @IBOutlet var textFieldMoreInfo: UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()

        textFieldItemName.delegate = self
        textFieldQt.delegate = self
        textFieldMoreInfo.delegate = self
    }

                       ...

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


   /**
    * Called when the user click on the view (outside the UITextField).
    */
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)
    }

}

( Source of this information ). 此信息的来源 )。


#5楼

override func viewDidLoad() {
    super.viewDidLoad()

    //Looks for single or multiple taps. 
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")

    //Uncomment the line below if you want the tap not not interfere and cancel other interactions.
    //tap.cancelsTouchesInView = false 

    view.addGestureRecognizer(tap)
}

//Calls this function when the tap is recognized.
@objc func dismissKeyboard() {
    //Causes the view (or one of its embedded text fields) to resign the first responder status.
    view.endEditing(true)
}

Here is another way to do this task if you are going to use this functionality in multiple UIViewControllers : 如果要在多个UIViewControllers使用此功能,则这是另一种执行此任务的方法:

// Put this piece of code anywhere you like
extension UIViewController {
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        tap.cancelsTouchesInView = false            
        view.addGestureRecognizer(tap)
    }

    @objc func dismissKeyboard() {
        view.endEditing(true)
    }
}

Now in every UIViewController , all you have to do is call this function: 现在,在每个UIViewController ,您所需要做的就是调用此函数:

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

This function is included as a standard function in my repo which contains a lot of useful Swift Extensions like this one, check it out: https://github.com/goktugyil/EZSwiftExtensions 该函数作为标准函数包含在我的仓库中,其中包含许多类似这样的有用的Swift扩展,请查看: https : //github.com/goktugyil/EZSwiftExtensions


#6楼

You can also add a tap gesture recognizer to resign the keyboard. 您还可以添加轻击手势识别器以使键盘退出。 :D :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")
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值