检测是否在UITableView中拖动键盘以关闭

时间:2017-03-24 06:57:58

标签: ios swift

我目前在我的桌面视图中有此设置。:

tableView.keyboardDismissMode = .interactive

我有一个通知观察员设置如下:

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

当键盘最终被拖出屏幕时,会调用我的keyboardWillHide方法。有没有办法检测键盘是如何被解雇的?调用keyBoardWillHide时,我需要知道是否因为按下了返回键,或者用户是否拖动它以便我可以调整动画。这有什么回调吗?

1 个答案:

答案 0 :(得分:0)

SetUp变量如:

var checker : Bool = false

设置keyBoardWillHide的通知:

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

Return按键返回键盘:

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

键盘将隐藏选择器调用:

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

希望这有帮助。