angeldev
angeldev

Reputation: 2053

Recieve two different height of keyboard after rotation

I have a view which is resize a constraint when the keyboard appear. So I have notifications when the keyboard appears and disappears.

The above behaviour occurs when the keyboard is already shown and I rotate the screen. Then the next actions occurs:

  1. UIKeyboardWillHideNotification called
  2. UIKeyboardWillShowNotification called (with the old height of the keyboard)
  3. UIKeyboardWillShowNotification called (with the new height of the keyboard)

So, the updateView function receives first one height and later a different height. This results in a weird movements of the view adjusting twice the value.

override func viewDidLoad() {

    super.viewDidLoad()

    // Creates notification when keyboard appears and disappears
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)

}

deinit {

    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)

}

func keyboardWillShow(notification: NSNotification) {

    self.adjustingHeight(true, notification: notification)

}

func keyboardWillHide(notification: NSNotification) {

    self.adjustingHeight(false, notification: notification)

}

private func adjustingHeight(show: Bool, notification: NSNotification) {

    // Gets notification information in an dictionary
    var userInfo = notification.userInfo!
    // From information dictionary gets keyboard’s size
    let keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
    // Gets the time required for keyboard pop up animation
    let animationDurarion = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSTimeInterval
    // Animation moving constraint at same speed of moving keyboard & change bottom constraint accordingly.
    if show {
        self.bottomConstraint.constant = (CGRectGetHeight(keyboardFrame) + self.bottomConstraintConstantDefault / 2)
    } else {
        self.bottomConstraint.constant = self.bottomConstraintConstantDefault
    }
    UIView.animateWithDuration(animationDurarion) {
        self.view.layoutIfNeeded()
    }

    self.hideLogoIfSmall()

}

Upvotes: 3

Views: 1044

Answers (1)

angeldev
angeldev

Reputation: 2053

Finally I found the solution. When I get keyboardFrame, I was using UIKeyboardFrameBeginUserInfoKey which returns the frame of the keyboard before the animation begins. The correct way of do that is with UIKeyboardFrameEndUserInfoKeywhich returns the frame of the keyboard after the animation has completed.

let keyboardFrame: CGRect = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()

Upvotes: 10

Related Questions