增加框架的大小 - Keyboardwillshow - Swift

时间:2017-12-25 02:46:26

标签: ios swift keyboard frame swift4

我的代码似乎在第一次调用键盘时工作正常。第二次调用键盘时,帧大小不会增加。

这是我的代码:

@objc func keyboardWillShow(sender: NSNotification) {

    let _: [NSObject : AnyObject] = sender.userInfo! as [NSObject : AnyObject]

    let keyboardSize: CGSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey]! as AnyObject).cgRectValue.size
    let offset: CGSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey]! as AnyObject).cgRectValue.size

    if keyboardSize.height == offset.height {
        UIView.animate(withDuration: 0.25, delay: 0.0, options: [.allowUserInteraction], animations: {
            let frame = self.view.frame
            self.view.frame = CGRect(x: 0, y: self.fullView, width: frame.width, height: frame.height)
        })

        noteDetails.frame.size.height = noteDetails.frame.size.height + keyboardSize.height
    } else {
        UIView.animate(withDuration: 0.25, delay: 0.0, options: [.allowUserInteraction], animations: {
            let frame = self.view.frame
            self.view.frame = CGRect(x: 0, y: self.fullView, width: frame.width, height: frame.height)
        })

        noteDetails.frame.size.height = noteDetails.frame.size.height + keyboardSize.height - offset.height
    }
}

@objc func keyboardWillHide(sender: NSNotification) {
    let _: [NSObject : AnyObject] = sender.userInfo! as [NSObject : AnyObject]

    let keyboardSize: CGSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey]! as AnyObject).cgRectValue.size

    noteDetails.frame.size.height = noteDetails.frame.size.height + keyboardSize.height

}

我添加了偏移量来检查自定义键盘高度并根据它增加高度。我觉得这导致代码崩溃。具体来说,第二次调用键盘时,keyboardSize的高度应该是自定义键盘的高度。不确定如何修复我的代码,以便表格的框架高度增加键盘的高度,无论是自定义键盘还是常规键盘。

1 个答案:

答案 0 :(得分:2)

使用UIKeyboardFrameEndUserInfoKey代替UIKeyboardFrameBeginUserInfoKey