Problem with uiscrollview setcontentoffset animated not scrolling when animated:yes is set

I had this happen too and ended up doing this workaround:

[UIView animateWithDuration:.25 animations:^{
    self.scrollView.contentOffset = ...;
}];

Speaking from personal experience, this problem occurs when setContentOffset: is getting called multiple times before the animation has had a chance to run.

I haven't totally tracked down the issue, but I believe the problem goes something like this:

setContentOffset:animated gets called which sets the new offset value. setContentOffset: subsequently gets called with the same parameters, which cancels the animation. It also notices that the new parameters are the same as the old parameters so assumes nothing needs to be done, even though the animation hasn't run yet.

The solution above somehow breaks this cycle, but perhaps a less kludgy solution is to put break points in the code and eliminate the multiple calls.


Hard to say with that piece of code you gave. And in fact, if you're saying, that you have a project, where everything is working under the same conditions, then obviously the conditions aren't the same :) I couldn't reproduce the issue, but here are some assumptions for 2 cases:

  1. Creation of UIScrollView with nib.

    • Just don't forget to create IBOutlet and to bound it to the scrollView element in nib. In a simple view based application just after that I got scrollView working as you wished in animated: both YES and NO. But to make work

    -(void)scrollViewDidScroll:(UIScrollView *)sender; you must set the delegate:

yourScrollView.delegate = self;

where 'self' is class, where you have your scrollViewDidScroll function; btw this class must conform to the 'UIScrollViewDelegate' protocol.

2.Creation of UIScrollView object manually.

  • here the idea is basically the same, so I'll just provide some simple code:

    -(void) creatingScrollView {
        UIScrollView *newScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 5, 280, 44)];
        newScroll.pagingEnabled = YES;
        newScroll.showsVerticalScrollIndicator = NO;
        newScroll.showsHorizontalScrollIndicator = NO;
    
        // ... some subviews being added to our scroll view
    
        newScroll.delegate = self;
        [self.view addSubview:newScroll];
        [newScroll release];
    }
    

This works for me. Hope this is helpful.