如何点击视图时如何显示键盘并开始编辑文本字段

时间:2018-05-29 02:49:42

标签: ios swift uitextfield becomefirstresponder

我正在尝试这样做,以便当用户触摸UIView键盘出现时,用户可以键入textField。我无法将视图作为第一响应者,我怎样才能实现这一目标?

以下是代码:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var searchBar: UITextField!
    var isKeyboardShowing:Bool = true
    var viewTapped:UITapGestureRecognizer = UITapGestureRecognizer()

    override func viewDidLoad() {
        super.viewDidLoad()
        searchBar.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -500).isActive = true
        viewTapped = UITapGestureRecognizer(target: self, action: #selector(viewWasTapped))
        self.view.addGestureRecognizer(viewTapped)


        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
        searchBar.delegate = self
    }

    override func viewWillAppear(_ animated: Bool) {
        self.searchBar.resignFirstResponder()
        self.view.becomeFirstResponder()
        print("Can the view be the first responder: \(self.view.canBecomeFirstResponder)")
        print("the searchbar is the first responder: \(searchBar.isFirstResponder)")
        print("the view is the first responder: \(self.view.isFirstResponder)")
    }

    @objc func viewWasTapped(){
    print("The view was tapped")
        if isKeyboardShowing{
            view.endEditing(true)
        }
        else{

        }
    }

    @objc func keyboardWillShow(notification: NSNotification) {
        print("keyboardWillShow")
    }

    @objc func keyboardWillHide(notification: NSNotification){
        print("keyboardWillHide")
    }

}

1 个答案:

答案 0 :(得分:2)

您不想让self.view成为第一响应者。您想让searchBar成为第一个响应者。

如果您打算在另一个视图上调用resignFirstResponder,则没有理由在一个视图上调用becomeFirstResponder

class ViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet weak var searchBar: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        searchBar.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -500).isActive = true
        searchBar.delegate = self

        let viewTapped = UITapGestureRecognizer(target: self, action: #selector(viewWasTapped))
        self.view.addGestureRecognizer(viewTapped)

        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }

    override func viewWillAppear(_ animated: Bool) {
        searchBar.becomeFirstResponder()

        print("Can the view be the first responder: \(self.view.canBecomeFirstResponder)")
        print("the searchbar is the first responder: \(searchBar.isFirstResponder)")
        print("the view is the first responder: \(self.view.isFirstResponder)")
    }

    @objc func viewWasTapped(){
        print("The view was tapped")
        if searchBar.isFirstResponder {
            searchBar.resignFirstResponder()
        } else{
            searchBar.becomeFirstResponder()
        }
    }

此外,searchBar并非UITextField的最佳名称。

viewTapped无需成为财产。使它成为局部变量。

不需要isKeyboardShowing属性。