如果我们触摸Swift中的UITextView之外的其他区域,如何关闭键盘

时间:2018-02-07 08:09:31

标签: ios swift uitableview uitextview uitapgesturerecognizer

我在静态表视图中有UITextView ,如果用户触摸textView 之外的其他区域,我想删除键盘

通常我可以通过这行代码删除键盘,但我不知道为什么,在textview中添加占位符后,它不起作用。我怀疑因为它是在表视图静态

extension CheckinDetailTVC {

    // to remove keyboard if we touch other area
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(false)
    }

}

我也尝试过stackoverflow中的另一个解决方案,但没有人在我这里工作,比如添加手势识别器

override func viewDidLoad() {
    super.viewDidLoad()
    let tapGestureReconizer = UITapGestureRecognizer(target: self, action: "tap:")
    view.addGestureRecognizer(tapGestureReconizer)
}

func tap(sender: UITapGestureRecognizer) {
    view.endEditing(true)
}

或在TextViewDelegate中添加函数,但它也不起作用

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    if text == "\n" {
        textView.resignFirstResponder()
    }
    return true
}

这是我的UI的截图

enter image description here

这是我在视图控制器中使用的简化代码

class CheckinDetailTVC: UITableViewController {


    @IBOutlet weak var noteTextView: UITextView!


    override func viewDidLoad() {
        super.viewDidLoad()

        // to put placeholder for UITextView
        noteTextView.delegate = self
        noteTextView.text = "Write your note in here ..."
        noteTextView.textColor = UIColor.lightGray
        noteTextView.becomeFirstResponder()
        noteTextView.selectedTextRange = noteTextView.textRange(from: noteTextView.beginningOfDocument, to: noteTextView.beginningOfDocument)



    }



}




extension CheckinDetailTVC : UITextViewDelegate {

// add placeholder for textView

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

        let currentText:String = textView.text
        let updatedText = (currentText as NSString).replacingCharacters(in: range, with: text)

        if updatedText.isEmpty {

            textView.text = "Write your note in here ..."
            textView.textColor = UIColor.lightGray

            textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument)

            return false
        }

        else if textView.textColor == UIColor.lightGray && !text.isEmpty {
            textView.text = nil
            textView.textColor = UIColor.black
        }

        return true
    }

    func textViewDidChangeSelection(_ textView: UITextView) {
        if self.view.window != nil {
            if textView.textColor == UIColor.lightGray {
                textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument)
            }
        }
    }

}




extension CheckinDetailTVC {

    // usually i can use this code to remove keyboard if we touch other area
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(false)
    }

}

这里出了什么问题?

3 个答案:

答案 0 :(得分:3)

选项1

不阻止用户界面添加cancelsTouchesInView  为你的手势`

let tapGestureReconizer = UITapGestureRecognizer(target: self, action: "tap:")
tapGestureReconizer.cancelsTouchesInView = false
view.addGestureRecognizer(tapGestureReconizer)

选项2

否则以多种方式处理辞职

func tap(sender: UITapGestureRecognizer) {
 view.endEditing(true)
 // or use 
 noteTextView.resignFirstResponder()
 // or use
 view.super().endEditing(true)
 // or use
 view.keyboardDismissMode = .onDrag
}

答案 1 :(得分:2)

正如上面提到的,使用UITapGestureRecognizer可以达到目的

extension UIViewController {

    @objc func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action:    #selector(UIViewController.dismissKeyboard))
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    }

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

然后在viewDidLoad

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

答案 2 :(得分:1)

您可以添加手势以通过触摸或滑动来关闭键盘。创建触摸手势对象并添加一个操作方法,使视图从第一响应者的状态中退出:

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(DismissKeyboard))
view.addGestureRecognizer(tap)

@objc func DismissKeyboard(){
//Causes the view to resign from the status of first responder.
view.endEditing(true)
}