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