如何在iOS swift中检测键盘高度变化或键盘更改?
看下面的代码,它在文本键盘和Emojis键盘中显示非常小的一行:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardHideNShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardHideNShow(_:)), name: UIKeyboardWillHideNotification, object: nil)
var didDisplayedKeyboard:Bool = false
func keyboardHideNShow(notification:NSNotification) {
var movement: CGFloat = 0.0
let userInfo:NSDictionary = notification.userInfo!
let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.CGRectValue()
let movementDuration:NSTimeInterval = 0.3
UIView.beginAnimations( "animateView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration )
if notification.name == UIKeyboardWillShowNotification {
// Do the operation only if its hide n show or show n hide
// When the keyboard switches from text …Run Code Online (Sandbox Code Playgroud)