Fixing UITextView on iOS 7
UITextView
on iOS 7 is a lot more powerful, since Apple switched over from using WebKit to TextKit for rendering. It’s also very much a 1.0, and has some rather terrible bugs. In fact, they go so far that people started writing replacements for the whole scrolling logic.
Of course, people reported these issues in PSPDFKit as well, so I had to find a workaround. I’m using contentInset
when the keyboard (iPhone) or another view (iPhone/iPad) goes up, which is pretty much completely ignored by UITextView
in iOS 7. This is frustrating mainly because it works perfectly in iOS 6.
At first, my solution was based on a category, but after discovering more and more needed hooks, I moved over to a subclass that automatically forwards all delegate methods. This has the advantage of more shared code, and we might be able to remove all those horrible hacks once iOS 8 comes out. I certainly hope so, and will write a few more radars.
So, what’s fixed in PSPDFTextView
?
- When adding a new line,
UITextView
will now properly scroll down. Previously, you needed to add at least one character for this to happen. - Scrolling to the caret position now considers
contentInset
.UITextView
completely ignored this. - Typing will also consider
contentInset
and will update the scroll position accordingly. - Pasted text will scroll to the caret position.
To enable these fixes, simply use PSPDFTextView
instead of UITextView
:
https://github.com/steipete/PSPDFTextView
This is working quite well for my use case, but there surely are edge cases where this won’t be enough (like when using rich text).
I also tried using the new textContainerInset
but this didn’t work as intended and didn’t solve my scrolling problems.
I have to give credit to countless people who searched for the same solution – this very much was a community-oriented fix. Sadly, this doesn’t seem to be a priority for Apple, since it’s still broken in iOS 7.1b3.
Please fork the repo and send a pull request if you have any ideas on how to simplify the code or find an even better workaround.