views:

190

answers:

1

I want to manually update the contentOffset of an UIScrollView during rotation changes. The scroll view fills the screen and has flexible width and flexible height.

I'm currently trying to update the contentOffset in willRotateToInterfaceOrientation, like this:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [Utils logPoint:myScrollView.contentOffset tag:@"initial"];
    myScrollView.contentOffset = CGPointMake(modifiedX, modifiedY);
    [Utils logPoint:myScrollView.contentOffset tag:@"modified"];
}

-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [Utils logPoint:myScrollView.contentOffset tag:@"final"];
}

However, the final value is not the modified value, and it kinda seems to be influenced by it, but it's not evident to me how.

These are some of the results that I get:

initial: (146.000000;-266.000000)
modified: (81.000000;-108.000000)
final: (59.000000;-0.000000)

initial: (146.000000;-266.000000)
modified: (500.000000;500.000000)
final: (59.000000;500.000000)

initial: (146.000000;-266.000000)
modified: (-500.000000;-500.000000)
final: (-0.000000;-0.000000)

How do I update the contentOffset of a scroll view during a rotation change?

+1  A: 

Converting my comment to an answer :)

Try changing contentOffset from within willAnimateRotationToInterfaceOrientation:duration: instead. An animation block should be in place by then, and the OS regards your changes to contentOffset as belonging to the changes caused by the rotation.

If you change contentOffset before, it looks like the system doesn't respect these changes as belonging to the rotation, and still applies rotation-resizing, this time starting from your new dimensions.

Pascal