6

How to detect Keyboard height change, or keyboard change in iOS swift?
See my below code, it shows very small line in text keyboard and Emojis keyboard:

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 to emoji, it wont hide the previous keyboard. will just replace
    //      In that case we need to avoid the keyboard movement
    if didDisplayedKeyboard == false {
        movement = -keyboardRectangle.height
        didDisplayedKeyboard = true
        print("m\(movement)")
        self.view.frame = CGRectOffset(self.view.frame, 0,  movement)
    }

} else if notification.name == UIKeyboardWillHideNotification {

    movement =  keyboardRectangle.height
    didDisplayedKeyboard = false
    self.view.frame = CGRectOffset(self.view.frame, 0,  movement)
}
UIView.commitAnimations()
}

How can I adjust my view?

D4ttatraya
  • 3,344
  • 1
  • 28
  • 50
Mathi
  • 109
  • 1
  • 1
  • 8
  • 2
    Observe the notification `UIKeyboardWillChangeFrameNotification` – zylenv Jul 29 '16 at 06:19
  • Thank you for your replay. i changed that in UIKeyboardWillChangeFrameNotification. but it wont work. send some example codes. – Mathi Jul 29 '16 at 07:15

3 Answers3

6

Use UIKeyboardWillChangeFrameNotification notification like this:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillChangeFrame(_:)), name: UIKeyboardWillChangeFrameNotification, object: nil)

And receive the changes as:

func keyboardWillChangeFrame(notification: NSNotification) {
    if let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
        let keyboardHeight = keyboardFrame.size.height
        print("keyboard height: \(keyboardHeight)")
        //do the chnages according ot this height
    }
}

This notification will give us the keyboard frame rect when keyboard appears, changes to emoji, shows/hides predictions!

D4ttatraya
  • 3,344
  • 1
  • 28
  • 50
3

Swift 4:

 NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChangeFrame), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)

Then, you can do something with the keyboard frame as such:

@objc func keyboardWillChangeFrame(_ notification: Notification) {
    if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
        let keyboardHeight = keyboardFrame.cgRectValue.size.height
        // Do something...
    }
}
ChrisRockGM
  • 368
  • 1
  • 5
  • 23
2

Swift 5:

NotificationCenter.default.addObserver(self, selector: #selector(_KeyboardHeightChanged(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)

and then change your view bottom constraint like this:

@objc private func _KeyboardHeightChanged(_ notification: Notification){
    if let frame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue{
        UIView.animate(withDuration: 0.5, animations: {
            self.SampleViewBottomConstaint.constant == 0 ? (self.SampleViewBottomConstaint.constant = frame.cgRectValue.height) : (self.SampleViewBottomConstaint.constant = 0)
        })

    }
}
Mr Zee
  • 87
  • 7