0

I've a table view below which I've text view. I scroll table view up with text view's height increases. The problem is that table view doesn't scroll up when return key is hit and cursor goes to new line, instead it scrolls up when a character is typed in this new line.

How do I fix so that table view scrolls up as soon as return key is pressed?

The pointed duplicate post is donkey ages old and solution is not in swift so that doesn't answer my question.

func textViewDidChange(_ textView: UITextView) {

    let estimatedSize = textView.sizeThatFits(textView.frame.size)
    
    if estimatedSize.height >= self.textViewMaxHeight {
        textView.isScrollEnabled = true
        textView.showsVerticalScrollIndicator = true
        let bottomOffset = CGPoint(x: textView.contentOffset.x, y: textView.contentSize.height - textView.bounds.size.height)
        textView.setContentOffset(bottomOffset, animated: false)
    } else {
        textView.isScrollEnabled = false
    }
    
    if self.dataSource.count > 0 {
        if tableView.contentOffset.y < (tableView.contentSize.height - tableView.frame.size.height) {
            self.tableView.scrollToBottom()
        }
    }
}
Raymond
  • 1,108
  • 13
  • 32

1 Answers1

-2

Check out this answer for dismissing the keyboard with the return key. Instead of calling the textView.resignFirstResponder() method, implement your scroll tableview to top logic. Generally it should look something like this:

myTableView.setContentOffset(.zero, animated:true)
AmirZ
  • 401
  • 6
  • 12
  • My question is not to dismiss keyboard. My question is how to scroll table view up when return key is pressed. – Raymond Jun 29 '20 at 18:13
  • True, but the way to implement it is the same. Use the textView: shouldChangeTextInRange: replacementText: to check if the replacement text equals "\n" which is the return key. Then implement your scroll to top logic there. – AmirZ Jun 30 '20 at 09:11
  • I've already tried moving logic inside new line detect block. It doesn't work. myTableView.setContentOffset(.zero, animated:true) also doesn't work. May be I'm missing something else. – Raymond Jun 30 '20 at 18:57
  • Are you sure you have connected the UITextView delegate? I have made a sample project with the code above and it works as expected. – AmirZ Jul 02 '20 at 15:27
  • Here, I made for you a demo app with a code sample that works. https://github.com/amirzucker/ScrollToTopOnReturnKey – AmirZ Jul 02 '20 at 15:48
  • Do note that both table view and textview delegates are set through the storyboard. – AmirZ Jul 02 '20 at 15:55
  • Delegates are set. In your demo app, text view doesn't scroll up with keyboard so what should I check? – Raymond Jul 02 '20 at 18:06
  • You want to scroll the textview or the tableview? From the way you worded the question it sounds like you wanna scroll the tableview, which the code example I provided does. – AmirZ Jul 02 '20 at 21:18
  • I want to scroll tableview as text view expands. Table view has to be above text view. – Raymond Jul 03 '20 at 16:25
  • Okey, the code I've provided does scroll the tableview when the return (or "enter" key) is tapped. I assume that this behaviour isn't what you wanted, and what you actually want, is some kind of machanism that scrolls the tableview whenever the textview starts a new line regardless if the return key was tapped. I also assume your textview has flexible size that expands based on the size of the text it presents. Is that correct? – AmirZ Jul 04 '20 at 16:16