2017-09-27 9 views
1

私はチャットアプリ(whatsappと思う)に取り組んでいます。 1つのビューコントローラでは、ユーザは送信する写真を選択し、そのメッセージをテキストフィールドに入力する。最初にテキストフィールド入力がキーボードの高さで移動します。それはうまく動作します。今、間違ったイメージを選択してクリックしてギャラリーや写真から別のイメージを選択した場合、テキストフィールドはキーボードで動かない。すぐにキーボードの高さが隠された

私のコードは次のとおりです。

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 

func keyboardWillShow(_ note: Notification) { 

    if let keyboardSize = note.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? CGRect { 

     let keyboardHeight = keyboardSize.height 
     let duration = (note.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Float) 
     let curve = (note.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! CInt) 

     //----------------Image Attachment---------------- 
     let imageX = self.imgBackgroundSend.frame.origin.x 
     let imageY = CGFloat(keyboardHeight) 
     let imageWidth = self.imgBackgroundSend.frame.size.width 
     let imageHeight = self.imgBackgroundSend.frame.size.height 

     self.imgBackgroundSend.frame = CGRect(x: imageX, y: imageY, width: imageWidth, height: imageHeight) 

     UIView.beginAnimations(nil, context: nil) 
     UIView.setAnimationBeginsFromCurrentState(true) 
     UIView.setAnimationDuration(CDouble(duration)) 
     UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: Int(CInt(curve)))!) 
     UIView.commitAnimations() 

     let offsetScrollPoint = CGPoint(x: CGFloat(0), y: CGFloat(keyboardHeight)) 
     self.scrlViewImageText.contentOffset = offsetScrollPoint 
    } 

} 


func keyboardWillHide(_ note: Notification) { 

    if let keyboardSize = (note.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { 
     let keyboardHeight = keyboardSize.height 

     let duration = (note.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double) 
     let curve = (note.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! CInt) 

     //----------------Image Attachment---------------- 
     let imageX = self.imgBackgroundSend.frame.origin.x 
     let imageY = self.scrlViewImageText.center.y - (self.imgBackgroundSend.frame.size.height/2) 
     print(keyboardHeight) 

     let imageWidth = self.imgBackgroundSend.frame.size.width 
     let imageHeight = self.imgBackgroundSend.frame.size.height 

     self.imgBackgroundSend.frame = CGRect(x: imageX, y: imageY, width: imageWidth, height: imageHeight) 

     UIView.beginAnimations(nil, context: nil) 
     UIView.setAnimationBeginsFromCurrentState(true) 
     UIView.setAnimationDuration(CDouble(duration)) 
     UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: Int(CInt(curve)))!) 
     UIView.commitAnimations() 

     let offsetScrollPoint = CGPoint(x: CGFloat(0), y: CGFloat(0)) 
     self.scrlViewImageText.contentOffset = offsetScrollPoint 
    } 

`

私は中学devのだと、それはおそらく

+0

この[リンク](https://stackoverflow.com/questions/45689664/ios-11-keyboard-height-is-returning-0-in-keyboard-notification/45689725#45689725)を試してみてください。それはあなたを助けるかもしれません。 –

+0

このhttps://github.com/hackiftekhar/IQKeyboardManagerを試してください – Himanth

+0

また、view.endEditing(true)を実行してviewWillAppearで最初のレスポンダとしてテキストフィールドを設定することで、ビューから離れて移動するときにキーボードを閉じることもできます。あなたのケースでキーボードアニメーションが大丈夫かどうかはわかりません。 –

答えて

0

アーディティヤSrivastava氏は正しかったされるほど簡単でこれを把握するために苦労しています - 変更しますUIKeyboardFrameEndUserInfoKeyが設定されたUIKeyboardFrameBeginUserInfoKeyが処理を行いました。

ありがとうございました。

私は、UITextViewを下向きではなくキーボードの下に展開するようにしたかったのです。このコードスニペットを使用しました。

var frame: CGRect = txtMessage.frame 
    frame.origin.y = frame.maxY - txtMessage.contentSize.height 
    frame.size.height = txtMessage.contentSize.height 
    txtMessage.frame = frame 
関連する問題