我目前在我的表格 View 上有这个设置。:
tableView.keyboardDismissMode = .interactive
我有一个像这样的通知观察者设置:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
我的 keyboardWillHide
方法在键盘最终被向下拖离屏幕时被调用。有没有办法检测键盘是如何被解除的?当调用 keyBoardWillHide
时,我需要知道是因为按下了返回键还是用户拖动了它,所以我可以调整动画。是否有任何类型的回调?
最佳答案
设置一个变量,如:
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
}
希望这对您有所帮助。
关于ios - 在 UITableView 中检测键盘是否被拖动以关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42993265/