键盘隐藏早于查看

时间:2019-04-22 13:58:38

标签: ios swift keyboard

键盘将作为子视图添加到集合视图控制器底部的容器中,但是当试图将其隐藏时,似乎键盘加载的时间比增加视图的大小要早(在键盘处于要隐藏)。 同样,当一天中第一次运行该项目时,直到我按“ command + K”,然后键盘上方和输入下方的键盘尺寸变暗时,键盘才会显示。 >

    @objc func handleShowKeyboard(_ notification: Notification) {
        if let keyboaradSize: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue? {
            let keyboardFram = keyboaradSize.cgRectValue
            UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
                self.view.frame.origin.y -= self.view.safeAreaInsets.bottom
                self.view.frame.origin.y -= keyboardFram.height
                self.view.layoutIfNeeded()

            }) { (completion) in

            }
        }
    }

    @objc func handleHideKeyboard(_ notification: Notification) {
        if let keyboaradSize: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue? {
            let keyboardFram = keyboaradSize.cgRectValue
            UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {

                self.view.frame.origin.y += self.view.safeAreaInsets.bottom
                self.view.frame.origin.y += keyboardFram.height
                self.view.layoutIfNeeded()

            }) { (completion) in

            }
        }
    }

enter image description here

1 个答案:

答案 0 :(得分:0)

阅读此键

UIKeyboardAnimationDurationUserInfoKey

值并将其用作动画持续时间


guard let userInfo = notification.userInfo else {
    return
} 

guard let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double,
    let curve =  userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? UInt else {

        return
}
UIView.animate(withDuration: duration, delay: 0, options: [UIView.AnimationOptions(rawValue: curve)], animations: {
   ///
}) { (completed) in

}