dynamic获取键盘的框架

是否有可能dynamic地获得键盘的框架,实际上是它的高度? 因为我有一个UITextView ,我想调整它的高度根据键盘框架的高度,当键盘的input方法改变。 如您所知,不同的input方法可能会有不同的键盘框架高度。

尝试这个:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil]; - (void)keyboardWasShown:(NSNotification *)notification { // Get the size of the keyboard. CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; //Given size may not account for screen rotation int height = MIN(keyboardSize.height,keyboardSize.width); int width = MAX(keyboardSize.height,keyboardSize.width); //your other code here.......... } 

教程了解更多信息

只要按照苹果的这个教程,你会得到你想要的。 Apple文档 。 为了确定键盘覆盖的区域,请参阅本教程 。

对于Swift 3用户来说,@Hector代码(有一些补充)是:

在你的viewDidLoad添加观察者:

 NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidShow(_:)), name: .UIKeyboardDidShow , object: nil) NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidHide(_:)), name: .UIKeyboardDidHide , object: nil) 

然后执行这些方法:

 func keyboardDidShow(_ notification: NSNotification) { print("Keyboard will show!") // print(notification.userInfo) let keyboardSize:CGSize = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue.size print("Keyboard size: \(keyboardSize)") let height = min(keyboardSize.height, keyboardSize.width) let width = max(keyboardSize.height, keyboardSize.width) } func keyboardDidHide(_ notification: NSNotification) { print("Keyboard will hide!") } 

您可以将此代码添加到包含Swift 3中的文本字段的视图。这将使文本字段与键盘一起上下移动。

 private var keyboardIsVisible = false private var keyboardHeight: CGFloat = 0.0 // MARK: Notifications private func registerForKeyboardNotifications() { NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) } private func deregisterFromKeyboardNotifications() { NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) } // MARK: Triggered Functions @objc private func keyboardWillShow(notification: NSNotification) { keyboardIsVisible = true guard let userInfo = notification.userInfo else { return } if let keyboardHeight = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.height { self.keyboardHeight = keyboardHeight } if !textField.isHidden { if let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber, let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber { animateHUDWith(duration: duration.doubleValue, curve: UIViewAnimationCurve(rawValue: curve.intValue) ?? UIViewAnimationCurve.easeInOut, toLocation: calculateTextFieldCenter()) } } } @objc private func keyboardWillBeHidden(notification: NSNotification) { keyboardIsVisible = false if !self.isHidden { guard let userInfo = notification.userInfo else { return } if let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber, let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber { animateHUDWith(duration: duration.doubleValue, curve: UIViewAnimationCurve(rawValue: curve.intValue) ?? UIViewAnimationCurve.easeInOut, toLocation: calculateTextFieldCenter()) } } } // MARK: - Helpers private func animateHUDWith(duration: Double, curve: UIViewAnimationCurve, toLocation location: CGPoint) { UIView.beginAnimations(nil, context: nil) UIView.setAnimationDuration(TimeInterval(duration)) UIView.setAnimationCurve(curve) textField.center = location UIView.commitAnimations() } private func calculateTextFieldCenter() -> CGPoint { if !keyboardIsVisible { return self.center } else { let yLocation = (self.view.frame.height - keyboardHeight) / 2 return CGPoint(x: self.center.x, y: yLocation) } }