【问题标题】:Increasing size of frame - Keyboardwillshow - Swift增加框架的大小 - Keyboardwillshow - Swift
【发布时间】:2017-12-25 02:46:26
【问题描述】:

我的代码在第一次调用键盘时似乎运行良好。第二次键盘被称为框架尺寸不增加。

这是我的代码:

@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 的高度应该是自定义键盘的高度。不知道如何修复我的代码,以便表格框架的高度随着键盘的高度增加,无论是自定义键盘还是常规键盘。

【问题讨论】:

  • 你试过我的答案了吗?想知道它是否有效

标签: ios swift keyboard frame swift4


【解决方案1】:

使用UIKeyboardFrameEndUserInfoKey 而不是UIKeyboardFrameBeginUserInfoKey

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-10
    • 2022-06-12
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    • 2016-01-22
    相关资源
    最近更新 更多