如何获取键盘的高度,包括Swift 4中的建议栏

Sav*_*les 11 keyboard ios swift

我用了 :

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
      }
}
Run Code Online (Sandbox Code Playgroud)

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

Iva*_*iuk 9

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

  • 不确定是否有某些变化,但在我的方法中,我使用这两种方法得到了完全相同的高度值,即使肯定存在建议栏。 (2认同)

Enr*_*dez 5

使用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
            }

        }
Run Code Online (Sandbox Code Playgroud)

  • 该方法只获取键盘的高度。我正在尝试获取包括建议栏在内的高度 (9认同)