UIScrollView setZoomScale:animated messes scrollVi

2019-03-04 02:50发布

问题:

I am developing an app with a view that holds a UISCrollView, inside which is a UIImageView.

When the view is about to appear, I perform a setZoomScale operation:

-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSLog(@"bounds of current view");
    NSLog(@"widht: %f, height: %f", self.view.bounds.size.width,self.view.bounds.size.height);
    NSLog(@"size of the image");
    NSLog(@"widht: %f, height: %f", self.imageView.image.size.width,self.imageView.image.size.height);
    NSLog(@"size of the scrollView BEFORE resizing");
    NSLog(@"widht: %f, height: %f", self.scrollView.contentSize.width,self.scrollView.contentSize.height);
    [self.scrollView setZoomScale:0.5 animated:NO];
    NSLog(@"size of the scrollView AFTER resizing");
    NSLog(@"widht: %f, height: %f", self.scrollView.contentSize.width,self.scrollView.contentSize.height);
}

The problem is that if the image is a square one, after applying setZoomScale the scrollView is not square anymore. Here is the output of running the above code:

2012-01-16 21:41:01.678 WorldTour[22781:f803] bounds of current view
2012-01-16 21:41:01.680 WorldTour[22781:f803] widht: 320.000000, height: 367.000000
2012-01-16 21:41:01.681 WorldTour[22781:f803] size of the image
2012-01-16 21:41:01.682 WorldTour[22781:f803] widht: 612.000000, height: 612.000000
2012-01-16 21:41:01.683 WorldTour[22781:f803] size of the scrollView BEFORE resizing
2012-01-16 21:41:01.683 WorldTour[22781:f803] widht: 612.000000, height: 612.000000
2012-01-16 21:41:01.684 WorldTour[22781:f803] size of the scrollView AFTER resizing
2012-01-16 21:41:01.685 WorldTour[22781:f803] widht: 306.000000, height: 259.500000

So, after running the code, a square scrollView of 612x612 turns out to be 306x259.5. What am I missing? What is going wrong?

Shouldn't [self.scrollView setZoomScale:0.5 animated:NO]; transform height and width the same way?

回答1:

Ok, after a lot of tries I finally got it working! I don't know exactly what was causing the problem, but with the scrollView selected in the storyboard I selected the Attributes Inspector inside XCode, and then unchecked "Autoresize Subviews". After unchecking it, it all works ok. The scrollView is correctly resized.

I solved this but don't understand what's causing the problem, so if you know why this happens feel free to comment!