I want to disable my back button while the user has the keyboard engaged.
Here's what I have so far:
The "backButton.isEnabled = false" disables the back button perfectly in "viewDidLoad":
class DetailViewController: UIViewController, UITextViewDelegate {
var backButton = UIBarButtonItem()
override func viewDidLoad() {
super.viewDidLoad()
backButton.title = "Save"
backButton.isEnabled = false
self.navigationController?.navigationBar.topItem?.backBarButtonItem = backButton
The below code works to trigger "keyboard is here" and "keyboard is gone" when the keyboard is called/dismissed:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
{
@objc func keyboardWillShow(notification: Notification) {
print("keyboard is here")
}
@objc func keyboardWillHide(notification: Notification) {
print("keyboard is gone")
}
However, when I try to drop the "backButton.isEnabled = false" in the "@objc func keyboardWillShow" function, nothing happens.
@objc func keyboardWillShow(notification: Notification) {
print("keyboard is here")
backButton.isEnabled = false
}
Not sure why the "backButton.isEnabled = false" works in the viewDidLoad but not keyboardWillShow... Any idea how to fix this?