Note that there are some explanatory texts on larger screens.

plurals
  1. POUnwanted automatic scrolling with UIScrollView and UITextFields as subviews
    primarykey
    data
    text
    <p><strong>The "too long; didn't read" version: <em>Is there any way to disable the automatic scrolling behaviour of <code>UIScrollView</code> when telling a <code>UITextField</code> to <code>becomeFirstResponder</code>?</em></strong></p> <p>I have a scroll view with paging enabled and several views as subviews, each subview being controlled by its own view controller. Each subview has a UITextField.</p> <p>The requirement is that when a page is scrolled into view, it's text field should become first responder.</p> <p>This is fine when using finger swipes to scroll -- I use the scroll view delegate method <code>scrollViewDidEndDecelerating:</code> to know when scrolling stops and a page is in view, I can tell the text field to become first responder.</p> <p>However, when the scroll view is "autoscrolled", as in when telling the scrollview to <code>scrollRectToVisible:animated:</code>, the scroll view delegate method for deceleration isn't called. I use this method when scrolling newly created pages into view without the user's interation, or when the user taps the <code>UIPageControl</code>.</p> <p>My solution was to simply set the first responder status of the text field before telling it to scroll into view - but it seems that telling a text field that is in a scroll view to become first responder causes the scroll view to automatically scroll it into view.</p> <p>I assume this is behaviour used when putting text fields in table view cells (since table views are scroll view subclasses). If you set up a small test app, with a table view, and a text field within a table cell, if the keyboard would obscure the table view cell when it becomes first responder, the table view will automatically scroll it to be visible.</p> <p>I don't understand, though, why this behaviour occurs in my example, where I'm not using a table view - just a plain scroll view.</p> <p>I should also mention that my scroll view has vertical scrolling disabled and only scrolls horizontally.</p> <p>I have tested in another test app that puts text fields as direct subviews of a scrollview (no view controllers or container views) and the same happens. If you tell a text field that is offscreen to become first responder, the scroll view with automatically scroll it for you.</p> <p>This wouldn't normally be a problem, but it seems to screw up the paging of the scroll view. When I scroll with my finger, each view bounces and is centred properly. But when I scroll a rect to be visible <em>with animation</em> and tell a text field to become first responder, scroll view seems to become conflicted with itself and the view is only scrolled part of the way into view, and isn't centred.</p> <p>Then, if I touch a view using my finger (not swipe, or even move), the scroll view jumps back to the first page.</p> <p>My current work around for all this silly auto scrolling behaviour is to use an <code>NSTimer</code> to determine when to update the first responder.</p> <p>I do the manual scrolling in code using <code>scrollRectToVisible:animated</code> and then after 0.3 seconds, call my method to update the text field to be first responder. (0.3 seconds was trial and error, trying to see which seemed to be the smallest amount of time to allow for the animation but still be long enough not to cause the conflict with the scrollview.</p> <p>As you can see, this isn't elegant, and is likely to break.</p> <p><strong>Is there any way to disable the automatic scrolling behaviour of <code>UIScrollView</code> when telling a <code>UITextField</code> to <code>becomeFirstResponder</code>?</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload