4

I currently have this setting on my table view.:

tableView.keyboardDismissMode = .interactive

I have a notification observer setup like so:

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

My keyboardWillHide method is called when the keyboard is finally dragged down and off the screen. Is there a way to detect how the keyboard was dismissed? When keyBoardWillHide is called I need to know if it was because the return key was pressed or if the user dragged it so I can tweak animations. Is there any sort of callback for this?

Kex
  • 6,762
  • 5
  • 39
  • 93

1 Answers1

0

SetUp a variable like:

var checker : Bool = false

Setting up notification for keyBoardWillHide:

override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: .UIKeyboardWillHide, object: nil)        
    }

Returning keyboard on Return key press:

 func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            checker = true
            textField.resignFirstResponder()
            return true
        }

Keyboard will hide selector call:

func keyboardWillHide (notif: Notification)
{
    if (checker == true)
    {
        print ("Return key pressed")
    }
    else
    {
        print ("Table dragged down")
    }
    checker = false
}

Hope this helps.

Md. Ibrahim Hassan
  • 4,681
  • 1
  • 19
  • 40