Why is UIScrollView's contentSize adjusted when zooming?

1k views Asked by At

With the code below, I see a red square appear which I can scroll around (because scrollview's content is 4x screen).

But once I start zooming out (scale < 1.0), the scrollview's contentSize immediately jumps to scale times the iPad's screen size. For example:

0.985783 757.081249 1009.441665

After which I cannot move the square around anymore. I can only move the square when scale get's above 1.0; in which case the scroll indicators show that the contentSize is still scale times screen size.

Another effect is that when zooming out below 1.0, the red square moves toward the top-left corner normally by zooming out. However, after I release the screen (without any further zooming), it moves even further to the corner. EDIT: This must be the rubber banding of the scrollview.

(The square itself does scale as expected.)

What I am missing here? Here's my view controller code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.scrollView                  = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    self.scrollView.contentSize      = CGSizeMake(self.view.bounds.size.width * 2, self.view.bounds.size.height * 2);
    self.scrollView.backgroundColor  = [UIColor colorWithWhite:0.5f alpha:1.0f];
    self.scrollView.minimumZoomScale = 0.1f;
    self.scrollView.maximumZoomScale = 4.0f;
    self.scrollView.clipsToBounds    = YES;
    self.scrollView.delegate         = self;

    self.contentView = [[UIView alloc] initWithFrame:self.scrollView.bounds];
    [self.scrollView addSubview:self.contentView];
    [self.view addSubview:self.scrollView];

    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(400, 400, 100, 100)];
    view.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:view];
}

#pragma UIScrollViewDelegate

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.contentView;
}

- (void)scrollViewDidEndZooming:(UIScrollView*)scrollView
                       withView:(UIView*)view
                        atScale:(double)scale
{
    NSLog(@"%f %f %f", scale, self.scrollView.contentSize.width, self.scrollView.contentSize.height);
}
0

There are 0 answers