UIKeyboardWillShowNotification 中的条形按钮项目动画

Bar button items animating in UIKeyboardWillShowNotification

我不知道哪里出了问题。我在导航栏中有 2 个右按钮,当键盘打开时我想要 A 和 B 按钮,当键盘关闭时,A 和 C,或者可能只是 C。我这样做了。我正在使用 UIKeyboardWillShowNotification 检查键盘何时打开或关闭。

 NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)

 NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)

它工作正常。问题是当我调用方法 "KeyboardWillShow" 和 "KeyboardWillHide" 时,正确的按钮飞进来了。请看这里:GIF

 func keyboardWillShow(sender: NSNotification) {
    if let userInfo = sender.userInfo {
        if let keyboardHeight = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue.size.height {
            textViewBottomConstraint.constant = keyboardHeight
            print("keyboard is shown")

            self.navigationItem.rightBarButtonItems = nil
            let rightButtons : NSArray = [self.keyboardRightButton, self.cameraRightButton]
            self.navigationItem.setRightBarButtonItems(rightButtons as? [UIBarButtonItem], animated: true)

            UIView.animateWithDuration(0.1, animations: { () -> Void in
                self.view.layoutIfNeeded()

            })
        }
    }


}

我试过这个,它工作正常,但只有在关闭键盘时才有效。

func dismissKeyboard()
{

    composeTextView.resignFirstResponder()

    self.navigationItem.rightBarButtonItems = nil
    self.navigationItem.setRightBarButtonItem(settingsRightButton, animated: false)

解决方法:

    func textViewShouldBeginEditing(textView: UITextView) -> Bool
{

    self.navigationItem.rightBarButtonItems = nil
    let rightButtons : NSArray = [self.keyboardRightButton, self.cameraRightButton]
    self.navigationItem.setRightBarButtonItems(rightButtons as? [UIBarButtonItem], animated: true)

    return true
}

谢谢