如何获得键盘的高度? (迅速)

不同的iOS设备上的键盘的高度是不同的。 有谁知道我怎样才能以编程方式获得设备的键盘的高度? 谢谢。

在Swift中:

您可以通过订阅UIKeyboardWillShowNotification通知来获取键盘高度。 (假设你想知道在显示之前的高度是多less)。

像这样的东西:

  NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil) 

然后你可以像这样访问一些函数中的高度:

Swift 2

 func keyboardWillShow(notification:NSNotification) { let userInfo:NSDictionary = notification.userInfo! let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue let keyboardRectangle = keyboardFrame.CGRectValue() let keyboardHeight = keyboardRectangle.height } 

Swift 3

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

和:

 func keyboardWillShow(_ notification: Notification) { if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue { let keyboardRectangle = keyboardFrame.cgRectValue let keyboardHeight = keyboardRectangle.height } } 

Swift 3.0:

1-在viewWillAppear方法中注册通知:

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

2-被调用的方法:

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

更新Swift 3.0

 private func setUpObserver() { NotificationCenter.default.addObserver(self, selector: .keyboardWillShow, name: .UIKeyboardWillShow, object: nil) } 

select器方法:

 @objc fileprivate func keyboardWillShow(notification:NSNotification) { if let keyboardRectValue = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue { let keyboardHeight = keyboardRectValue.height } } 

延期:

 private extension Selector { static let keyboardWillShow = #selector(YourViewController.keyboardWillShow(notification:)) } 

小费

UIKeyboardDidShowNotification或UIKeyboardWillShowNotification可能会调用两次,得到不同的结果,这篇文章解释了为什么调用两次。

在Swift 2.2中

Swift 2.2弃用了select器的string,而是引入了新的语法: #selector

就像是:

 private func setUpObserver() { NSNotificationCenter.defaultCenter().addObserver(self, selector: .keyboardWillShow, name: UIKeyboardWillShowNotification, object: nil) } 

select器方法:

 @objc private func keyboardWillShow(notification:NSNotification) { let userInfo:NSDictionary = notification.userInfo! let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue let keyboardRectangle = keyboardFrame.CGRectValue() let keyboardHeight = keyboardRectangle.height editorBottomCT.constant = keyboardHeight } 

延期:

  private extension Selector { static let keyboardWillShow = #selector(YourViewController.keyboardWillShow(_:)) } 

这里较短的版本:

 func keyboardWillShow(notification: NSNotification) { if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() { let keyboardHeight = keyboardSize.height } } 

//第1步:注册通知中心

 ViewDidLoad() { self.yourtextfield.becomefirstresponder() // Register your Notification, To know When Key Board Appears. NotificationCenter.default.addObserver(self, selector: #selector(SelectVendorViewController.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) // Register your Notification, To know When Key Board Hides. NotificationCenter.default.addObserver(self, selector: #selector(SelectVendorViewController.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) } 

//第2步: – 这些方法将在键盘出现或隐藏时自动调用

  func keyboardWillShow(notification:NSNotification) { let userInfo:NSDictionary = notification.userInfo! as NSDictionary let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue let keyboardRectangle = keyboardFrame.cgRectValue let keyboardHeight = keyboardRectangle.height tblViewListData.frame.size.height = fltTblHeight-keyboardHeight } func keyboardWillHide(notification:NSNotification) { tblViewListData.frame.size.height = fltTblHeight }