0

I can't figure it out what is wrong. I have 2 right buttons in the nav bar, and when the keyboard is On I want A & B buttons, and when the keyboard is Off, A and C, or maybe just C. I did this. Im using UIKeyboardWillShowNotification to check when the keyboard is on or off.

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

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

It works fine. The problem is when I call the methods "KeyboardWillShow" and "KeyboardWillHide" the right buttons fly in. See here: 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()

            })
        }
    }


}

I tried with this, it works fine, but only when dismissing the keyboard.

func dismissKeyboard()
{

    composeTextView.resignFirstResponder()

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

1 Answers1

0

Solved with:

    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
}

Thanks