After some serious debugging I finally found the culprit... There is actually no reason for scrollRectToVisible
to not work with UITextViews
, since it take as parameter a CGRect
.
I actually had two problems:
I used
textView.bounds.origin.y
to create the CGRect for the scrolling method, which is not correct. thebounds
property returns coordinates relative to the object itself, not the parent view, so I was actually passing a 0 as height, hence no scrolling was done. To use the coordinate relative to the parent view, you must usetextview.frame.origin.y
.I had my text view inside a content view which was in turn inside the scrollview, not being the same height as the scrollview. This is a problem since the
frame
property will give you only values relative to the parent view, so this wasn't the realy
for the whole main view. To fix this, useconvertRect:toView:
and that will make sure to pass the correcty
value relative to the view specified to theCGRect
inscrollRectToVisible
.
After fixing these two issues, text views scrolled smoothly and neatly into place when the keyboard was shown on top of them.
I've seen many people asking about this and no real concise answer has been given. Entire classes and big solutions have been implemented, which are not even necessary.
Anyway, I hope this becomes useful for someone in the future...