在UIKeyboardWillShowNotification中设置动画的按钮项目

时间:2016-02-05 00:30:40

标签: ios swift rightbarbuttonitem

我无法弄清楚出了什么问题。我在导航栏中有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)

1 个答案:

答案 0 :(得分:0)

解决:

    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
}

由于