Sfoglia il codice sorgente

hide keyboard if table view is scrolled up

cyberta 4 anni fa
parent
commit
60a518959e
1 ha cambiato i file con 20 aggiunte e 0 eliminazioni
  1. 20 0
      deltachat-ios/Chat/ChatViewControllerNew.swift

+ 20 - 0
deltachat-ios/Chat/ChatViewControllerNew.swift

@@ -18,6 +18,8 @@ class ChatViewControllerNew: UITableViewController {
     var ephemeralTimerModifiedObserver: Any?
 
     var originFrame: CGRect?
+    var lastContentOffset: CGFloat = -1
+    var isKeyboardShown: Bool = false
 
     /// The `InputBarAccessoryView` used as the `inputAccessoryView` in the view controller.
     open var messageInputBar = InputBarAccessoryView()
@@ -143,9 +145,14 @@ class ChatViewControllerNew: UITableViewController {
                                        object: nil)
         notificationCenter.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
         notificationCenter.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
+        notificationCenter.addObserver(self, selector: #selector(keyboardDidShow(_:)), name: UIResponder.keyboardDidShowNotification, object: nil)
         prepareContextMenu()
     }
 
+    @objc func keyboardDidShow(_ notification: Notification) {
+        isKeyboardShown = true
+    }
+
     @objc func keyboardWillShow(_ notification: Notification) {
         if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
            tableView.inputAccessoryView?.frame.height ?? 0 < keyboardSize.height {
@@ -168,6 +175,7 @@ class ChatViewControllerNew: UITableViewController {
            let originFrame = originFrame {
             self.tableView.frame = originFrame
         }
+        isKeyboardShown = false
     }
 
     private func startTimer() {
@@ -361,10 +369,22 @@ class ChatViewControllerNew: UITableViewController {
         return cell
     }
 
+    public override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
+        lastContentOffset = scrollView.contentOffset.y
+    }
+
     public override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
         if !decelerate {
             markSeenMessagesInVisibleArea()
         }
+
+        if scrollView.contentOffset.y < lastContentOffset {
+            if isKeyboardShown {
+                tableView.endEditing(true)
+                tableView.becomeFirstResponder()
+            }
+        }
+        lastContentOffset = -1
     }
 
     public override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {