I am trying to make it so that when the user touches the UIView
the keyboard appears and the user can type in the textField. I am unable to make the view a first responder how can I make this possible?
Below is the code:
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")
}
}