【问题标题】:how to get the height of the keyboard including the suggestions bar in swift 4如何获得键盘的高度,包括swift 4中的建议栏
【发布时间】:2018-11-18 11:21:47
【问题描述】:

我用过:

NotificationCenter.default.addObserver(self, selector:#selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)

@objc func keyboardWillShow(notification: NSNotification) {
      if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
      let keyboardHeight : Int = Int(keyboardSize.height)
      print("keyboardHeight",keyboardHeight)
      KeyboardHeightVar = keyboardHeight
      }
}

更改以获得键盘的高度,但高度不包括建议栏如何获取键盘高度加上建议栏高度的值?

【问题讨论】:

标签: ios swift keyboard


【解决方案1】:

使用UIKeyboardFrameEndUserInfoKey 而不是UIKeyboardFrameBeginUserInfoKey 会返回正确的键盘高度。 例如,如果键盘没有工具栏,则返回 216.0 高度。使用工具栏 - 260.0

【讨论】:

  • 第一次调用两次,其余时间调用一次。有什么原因吗?
  • 嗯,我没有这个问题。在viewDidLoad 你可以这样做:NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
  • 反正keyboardWillshow方法被调用一次吗?
  • 不确定是否有任何变化,但在我的身上,我使用这两种方法得到的高度值完全相同,尽管肯定存在建议栏。
  • 对于 Swift4 4/5 我使用它并且它有效:让 userInfo = notification.userInfo ?? [:] let keyboardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as!NSValue).cgRectValue let height = keyboardFrame.height
【解决方案2】:

使用UIKeyboardFrameEndUserInfoKey 代替UIKeyboardFrameBeginUserInfoKeyUIKeyboardDidShow 代替UIKeyboardWillShow

NotificationCenter.default.addObserver(self, selector: 

#selector(keyboardWillShow), name: .UIKeyboardDidShow, object: nil)
    @objc func keyboardWillShow(notification: NSNotification) {

            if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
                let keyboardHeight : Int = Int(keyboardSize.height)
                print("keyboardHeight",keyboardHeight)
                KeyboardHeightVar = keyboardHeight
            }

        }

【讨论】:

  • 该方法只获取键盘的高度。我正在尝试获取高度,包括建议栏
【解决方案3】:

尝试改用UIKeyboardDidShow

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

只要键盘出现在屏幕上,你就会在keyboardWasShown方法中得到回调,

@objc func keyboardWasShown(_ notification : Notification)
{
    let info = (notification as NSNotification).userInfo
    let value = info?[UIKeyboardFrameEndUserInfoKey]
    if let rawFrame = (value as AnyObject).cgRectValue
    {
        let keyboardFrame = self.reportItTableView.convert(rawFrame, from: nil)
        let keyboardHeight = keyboardFrame.height //Height of the keyboard
    }
}

【讨论】:

  • 在这种情况下,我们失去了流畅的动画!
  • 这个答案对我有帮助,我正在使用键盘框架确实更改通知并检索框架最终用户信息值,但没有将键盘框架转换为封闭视图,一旦我这样做了,我就得到了正确的结果,并且可以正确滚动视图。
  • 这个方法被调用了 3 次,给了我 3 个不同的高度。
【解决方案4】:

首先,您需要注册在键盘可见时触发的通知。

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)

在方法中获取键盘高度...

@objc func keyboardWillShow(_ notification: Notification) {

 if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
    let keyboardRectangle = keyboardFrame.cgRectValue
    let keyboardHeight = keyboardRectangle.height
 }
}  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 2013-08-28
    • 2018-03-17
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多