1

okay so this question is similar to this one , but i followed answer of that question but i didn't worked , so the thing is that i have a Textfield in my view and i want to move it up when keyboard appears this is my code :

Notification observer for keyboard's state

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

function for getting the keyboard's height

    func keyboardWillShow(notification:NSNotification) {
    let userInfo:NSDictionary = notification.userInfo!
    duration = (notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double)
    let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
    let keyboardRectangle = keyboardFrame.CGRectValue()

    keyboardHeight = keyboardRectangle.height

}

my animation

    func textFieldDidBeginEditing(textField: UITextField) {

    self.nextButtonConstraint.constant  = keyboardHeight
    UIView.animateWithDuration(duration) {
        self.nextButton.layoutIfNeeded()
        self.emailTextField.layoutIfNeeded()

    }

}

as you can see that my animation is on textFieldDidBeginEditing because according to similar question's answer putting it there will solve the problem , but still on first run (when keyboard appears for the first time) my animation is not smooth

Community
  • 1
  • 1
remy boys
  • 2,928
  • 5
  • 36
  • 65
  • Is this code within a `UIViewController` subclass? Try calling `self.view.layoutIfNeeded` instead of calling it on `nextButton` and `emailTextField`. – Connor Neville Aug 18 '16 at 16:13
  • @ConnorNeville , thanks for responding man yeah they are inside of `UIViewController` Class , and i tired your suggestion still no change – remy boys Aug 18 '16 at 16:37
  • Hmm. Try adding another `NSNotificationCenter` observer for `keyboardDidShow`, and put your animation in there. – Connor Neville Aug 18 '16 at 16:42

1 Answers1

1

I think the solution may be quite simple. Try to put all you Code inside of this:

  DispatchQueue.main.async{
    // Your Code here
  }

This moves the animation to the main Thread, which should speed it up and avoid the unsmooth animation.

Nils
  • 1,755
  • 3
  • 13
  • 25