【问题标题】:Detecting if the keyboard is dragged to dismiss in UITableView在 UITableView 中检测键盘是否被拖动以关闭
【发布时间】:2017-03-24 06:57:57
【问题描述】:

我目前在我的表格视图中有此设置。:

tableView.keyboardDismissMode = .interactive

我有一个这样的通知观察者设置:

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

我的keyboardWillHide 方法在键盘最终被向下拖离屏幕时被调用。有没有办法检测键盘是如何被解雇的?当keyBoardWillHide 被调用时,我需要知道是因为按下了返回键还是用户拖动了它以便我可以调整动画。对此有任何回调吗?

【问题讨论】:

标签: ios swift


【解决方案1】:

设置一个变量,如:

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
}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2012-04-04
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 2020-07-25
    相关资源
    最近更新 更多