user12174378
user12174378

Reputation:

Not able to detect keyboard going away?

I am trying to detect when the keyboard is disappearing so I can lower a text field. How can I modify my code to do this?

Methods for detecting changes:

    extension NotificationsViewController {

    @objc func keyboardWillChange(notification: Notification) {
        print("something djkshbfkhsalk")

        guard let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }

        if notification.name == UIResponder.keyboardWillShowNotification || notification.name == UIResponder.keyboardWillChangeFrameNotification {
            messegeBox.frame.origin.y = (messegeBox.frame.origin.y-(keyboardRect.height/2))+5
        } else {
            messegeBox.frame.origin.y = messegeBox.frame.origin.y+(keyboardRect.height/2)-5
        }

    }

    @objc func keyboardWillShow(notification: Notification) {  
    }

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

    @objc func keyboardWillDisapear(notification: NSNotification) {  
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        print("fdsafdsafdsafdsafdsaf")
        msgTextField.resignFirstResponder()
        return true
    }

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        msgTextField.resignFirstResponder()
    }

}

Setup the first happens in the viewdidload, the second is its own method:

        //Listen for keyboard events
    NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController.keyboardWillChange(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController.keyboardWillChange(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController.keyboardWillChange(notification:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)

}

deinit {
    //Stop listening for keyboard hide/show events
    NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
    NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}

Thanks for any help

Upvotes: 0

Views: 78

Answers (1)

Rohit Pradhan
Rohit Pradhan

Reputation: 3875

You can update these to methods

NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController.keyboardWillChange(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController.keyboardWillChange(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)

to

  NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController. keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
  NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController. keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)

and you will get the events in the existing methods you have written

     @objc func keyboardWillShow(notification: Notification) {
             //Called before keyboard shows
    //Get the keyboard size like this
    if let keyboardSize = (notification.userInfo? [UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        }
      }

      @objc func keyboardWillHide(notification: NSNotification) {
             //Called before keyboard hides
      }

Using these methods you can change the frames or scroll

Upvotes: 2

Related Questions