何が悪いのかわかりません。ナビゲーション バーには 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)