1

so i'm showing a button on top of my keyboard when keyboard is appearing and then hiding it when keyboard dismissed but the flow of animation is not smooth enough

i want to it come up and down with keyboard (without blocking the UI)

images:

WHEN KEYBOARD APPEARS , YOU CAN SEE THAT KEYBOARD IS STILL NOT FULLLY APPEARED BUT MY BUTTON IS HERE

enter image description here

AND WHEN IT HIDES , STILL NOT FULLY DISMISSED BUT BUTTON IS GONE

enter image description here

my code :

CODE:

viewDidLoad ...... {
        // here we have notification observers for tracking the states of Keyboard
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LaunchScreenViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LaunchScreenViewController.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)

}


    func keyboardWillShow(notification:NSNotification) { // in this function i'm changing the origin (Y) axis of my button os that it can appear on top of my keyboard 
    let userInfo:NSDictionary = notification.userInfo!
    let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
    let keyboardRectangle = keyboardFrame.CGRectValue()
    UIView.animateWithDuration(0.3) {

        self.nextButtonConstraint.constant  = keyboardRectangle.height
    }
}

func keyboardWillHide(notification:NSNotification) {
    UIView.animateWithDuration(0.5) {

        self.nextButtonConstraint.constant  = -50 // here i'm making my button  out of screen bounds 
    }
}
remy boys
  • 2,928
  • 5
  • 36
  • 65

1 Answers1

2

You should apply correct animation:

self.nextButtonConstraint.constant  = keyboardRectangle.height
UIView.animateWithDuration(0.3) {
    <outlet to nextButton>.layoutIfNeeded() // insert correct value in <>
}

And don't use 0.3, but take timeInterval from UIKeyboardAnimationDurationUserInfoKey

Igor
  • 12,165
  • 4
  • 57
  • 73
  • hey man its working fine , but what with the `UIKeyboardAnimationDurationUserInfoKey` ?? – remy boys Aug 17 '16 at 13:12
  • you get exact timeInterval for animation of keyboard appearence (usually 0.25) – Igor Aug 17 '16 at 13:14
  • but how to get it from `UIKeyboardAnimationDurationUserInfoKey` ? its a string right ? `public let UIKeyboardAnimationDurationUserInfoKey: String // NSNumber of double` – remy boys Aug 17 '16 at 13:15
  • It's NSTimeInterval – Igor Aug 17 '16 at 13:15
  • okay got it `let duration: NSTimeInterval = (notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSTimeInterval)` – remy boys Aug 17 '16 at 13:18
  • or `let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as Double` – Igor Aug 17 '16 at 13:19
  • I just wanted to add that for me duration doesn't work with above calculation. A value of 0.03 is working for me which is way off from 0.25. I wonder if there is any exact solution at all? – Raymond May 19 '20 at 22:27