0

I have a content view inside my scroll view that has a small height. The scroll view takes up the entire height of the containing view controller. The bottom most view in the content view is a text view. When the user taps on the text view the keyboard animates up and I animate the bottom anchor of the scroll view up with it. Now the scroll view is short enough to where the content view can scroll. I want to "focus" on the text view so I find its origin relative to the scroll view and use setContentInset(_,animated:) to scroll the text view onto the screen if it isn't already on screen. I do that with this code:

let point = self.scrollView.convert(self.textView.frame.origin, to: self.scrollView)
self.scrollView.setContentOffset(point, animated: true)

The problem here is that there is extra "dead" space between the bottom of the text view and the top of the keyboard. Yet setContentInset scrolls to this state nonetheless. When I go to scroll the content view, it snaps down to the top of the keyboard.

here is a visual description. The orange view is the scroll view and the dark gray view represent the top of the keyboard. As you can see, setting the content offset of the scroll view to the origin of the light gray text view means that there is an amount of "dead" (not sure what to call this or how to think about it) space between the end of the scroll view's content and the top of the "keyboard". Because of this, when you go to scroll the scroll view, it immediately snaps the text view to the keyboard.

What can I do to remedy this?

Alex Bollbach
  • 4,370
  • 9
  • 32
  • 80
  • See https://stackoverflow.com/a/7060375/1305067 and https://stackoverflow.com/a/1521140/1305067; you can set the content inset to a position past the content size, but it will snap back when you touch the content view. The best option would be to either change the content size, or just not set the content inset past the content size. – paulvs Jul 21 '17 at 20:19
  • i'm trying to avoid the snap back. fundamentally, if i set content offset to the top of the bottom most view in the content and that view is less tall than the scroll view's height, there will be this "dead" space. – Alex Bollbach Jul 21 '17 at 20:20
  • That's correct, and I don't think the "snap back" can be turned off. A workaround is to change the content size to artificially to accommodate the "dead space" you're wanting below your bottom view. Alternatively, you can change your requirements, and only scroll the bottom of the bottom view to the top of the keyboard. – paulvs Jul 21 '17 at 20:33

0 Answers0