Jamie - Decodefy Ltd
Jamie - Decodefy Ltd

Reputation: 1397

zoomScaled UIScrollView positioning incorrectly when field tapped

I have a strange issue with a UIScrollView when it's zoomed in.

We display user-created forms on the iPad within this UIScrollView, so the fields can be of varying size. The positioning seems to handle well enough for moving the fields up (by adding content offsets when keyboard appears) but the act of tapping the field also appears to be doing some kind of positioning.

My problem with this is - if the user is zoomed in to the point where a field is wider than the view, tapping the field (even if the keyboard is already visible) scrolls the view all the way to the right, despite being left-to-right text entry with no text in the field yet. This pushes the left of the field (where the user is expecting to type) off the left of the screen.

Note - as mentioned above, this also happens if the keyboard is already visible and the user is already editing the field. Tapping the field again (to display the Delete / Copy / Paste etc) also shoves the scroll view over to the right, thus cutting off the first part of the field.

The ideal solution would be to have this instead scroll to make the caret position visible on screen, but can't find anything related to this issue or any evidence that anyone else has seen it on SO or google.

Example - Example position

Upvotes: 0

Views: 131

Answers (1)

wj2061
wj2061

Reputation: 6885

You can locate the problem by adding a Symbolic Breakpoint:

[UIScrollView setContentOffset:animated:]

enter image description here

enter image description here

Run your app again and enable the breakpoint just before the bug happens.

And when you trigger the break point, you can see a thread trace like this:

enter image description here

So the problem is when the UITextField calls becomeFirstResponder, it also call [UIScrollView scrollRectToVisible:animated:] with the textfield's frame rect in Scrollview,if it's contained in a UIScrollView.

And if you call [UIScrollView scrollRectToVisible:animated:] with the textfield's frame rect in Scrollview manully, you can recreate this bug.

To fix this issue, you can subclass UIScrollView,and change the behaviour of [UIScrollView scrollRectToVisible:animated:] to show the left side of the rect, or don't scroll if the rect is equal to one of its subclass (also UITextField)'s frame.

Upvotes: 2

Related Questions