2017-03-10 7 views
0

私はメッセンジャーアプリを構築しています。キーボード通知に対するスクロールUICollectionView

次のように私の現在のキーボード通知機能がある:

func handleKeyboardNotification(notification: NSNotification) { 

     if let userInfo = notification.userInfo { 
      let keyboardFrame = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue() 
      print(keyboardFrame) 


      let isKeyboardShowing = notification.name == UIKeyboardWillShowNotification 


      bottomConstraint?.constant = isKeyboardShowing ? -keyboardFrame!.height : 0 



      UIView.animateWithDuration(0, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: { 

       self.view.layoutIfNeeded() 
       }, completion: { (completed) in 

        if isKeyboardShowing { 
         let indexPath = NSIndexPath(forItem: self.messages!.count - 1, inSection: 0) 
         self.collectionView?.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true) 
        } 

      }) 

    } 
} 

bottomConstraint.constantは、画面の下部に、またはキーボードの上のいずれかであるメッセージのテキスト入力フィールドへの参照でありますしかし、キーボードが開いた後ではなく、キーボードのオープニングと同時に完了パラメータのアクションを発生させます。現在、キーボードを閉じると、それを開いたときに自動レイアウトが正しく表示されないようです。

私は、これまで多くの方法についてこれまで役に立たなかった。 提案がありますか?

答えて

1

補完されたコードブロックがキーボードのダウンと同時に発生するようにするには、補完ブロックに書き込むべきではありません。 あなたが継続機能でアニメーションを呼び出す前に、同時になるように、それがうまくいくかもしれないので、あなたはdispatch_asyncを使用できます:私は私のキーボードが登場したときにも移動することScrollViewを必要

func handleKeyboardNotification(notification: NSNotification) { 

      if let userInfo = notification.userInfo { 
       let keyboardFrame = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue() 
       print(keyboardFrame) 


       let isKeyboardShowing = notification.name == UIKeyboardWillShowNotification 


       bottomConstraint?.constant = isKeyboardShowing ? -keyboardFrame!.height : 0 

DispatchQueue.main.async { 
    if isKeyboardShowing { 
        let indexPath = NSIndexPath(forItem: self.messages!.count - 1, inSection: 0) 
          self.collectionView?.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true) 
} 


      UIView.animateWithDuration(0, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {self.view.layoutIfNeeded()}, completion: nil}) 

    } 
} 
+0

ので、私は上記の回答のバージョンを使用しました: ** DispatchQueue.main.async { self.scrollToBottomAnimated(animated:true) ** – Ernie

関連する問題