Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with setContentOffset:animated:YES
    text
    copied!<p>I'm having a problem with what almost seems like a bug in iOS. I'm trying to do some really simple scrolling in a UIScrollView. If I scroll to a point with an animation, it scrolls there perfectly fine, but it <em>doesn't set the point to the scrollView</em>. I.E. when I scroll to somewhere else later, it jumps up to 0,0 and starts the animation from there.</p> <p>I'm using the following code</p> <pre><code>[scrollView setContentOffset:CGPointMake(0, 95) animated:YES]; NSLog(@"offset x %@", [[NSNumber numberWithFloat:scrollView.contentOffset.x] stringValue]); NSLog(@"offset y %@", [[NSNumber numberWithFloat:scrollView.contentOffset.y] stringValue]); </code></pre> <p>which produces output</p> <pre><code>offset x 0 offset y 0 </code></pre> <p>while the exact same code with the animation off:</p> <pre><code>[scrollView setContentOffset:CGPointMake(0, 95) animated:NO]; NSLog(@"offset x %@", [[NSNumber numberWithFloat:scrollView.contentOffset.x] stringValue]); NSLog(@"offset y %@", [[NSNumber numberWithFloat:scrollView.contentOffset.y] stringValue]); </code></pre> <p>produces output</p> <pre><code>offset x 0 offset y 95 </code></pre> <p>I'm trying to automatically scroll to a UITextView so I'm listening to some keyboard notifications where I normally do the scrolling. But I've done this test in viewDidLoad and it produces these results.</p> <p>Doing scrollView.contentOffset = CGPointMake(0,95); also sets the value correctly. It's just the animated one that doesn't.</p> <p>Edit: The code I am actually trying to run is this:</p> <pre><code>- (void)textFieldDidBeginEditing:(UITextField *)textField { //60 is half the available space in portrait mode so it puts the textfield in the centre. [self.scrollView setContentOffset:CGPointMake(0, textField.frame.origin.y-60) animated:YES]; } </code></pre> <p>Which scrolls the view to the correct position. But since it doesn't seem to set contentOffset correctly it starts the animation from 0,0 all the time. Now matter how long I wait between the animations.</p>
 

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