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

How do I make the keyboard appear and the text field begin editing when the view is tapped

我试图做到这一点,以便当用户触摸 UIView 时会出现键盘,用户可以在文本字段中键入内容。我无法将视图设置为第一响应者,我怎样才能做到这一点?

代码如下:

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")
    }

}

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

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

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 属性.