tags:

views:

31

answers:

1

I have a UIScrollView with a UIImage inside. The following code initializes the image properly (it's width is the same as the width of the scrollView):

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    ...

    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"map1.jpg"]];
    imageView.userInteractionEnabled = YES;
    imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth );
    [imageScrollView addSubview:imageView];

    imageScrollView.contentSize = [imageView frame].size;

    ...

    // calculate minimum scale to perfectly fit image width, and begin at that scale
    float minimumScale = [imageScrollView frame].size.width  / [imageView frame].size.width;
    //imageScrollView.maximumZoomScale = 1.0;
    imageScrollView.minimumZoomScale = minimumScale;
    imageScrollView.zoomScale = minimumScale;
}

What I want to do is to set the size to perfectly fit (like above) when the device rotates. Here's what I've tried to do:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
  float newScale = [imageScrollView frame].size.width  / [imageView frame].size.width;
  imageScrollView.zoomScale = newScale;
}

For some reasons this doesn't work at all. The only way that I was able to make it work was to initialize another UIImage and do all the initialization process again, which doesn't feel right to me.

A: 

Probably you should also change .contentSize of scroll view.

Nickolay O.
It doesn't seem to work unfortunately. I think the problem is with the UIImageView, not the UIScrollView.
Oscar Del Ben
Ok, please describe what do you see on screen after rotation, i.e. what is the size of UIImage and so on?
Nickolay O.
The image is zoomed in (way more than necesary)
Oscar Del Ben
That's because of UIViewAutoresizingFlexibleWidth - image is resized proprtionally screen width change (after rotation). Check image frame, and change it to the value it has at the beginning.
Nickolay O.
Thanks for you help Nickolay. Unfortunately I wasn't able to follow your advice. How should I change the image frame?
Oscar Del Ben
You should set imageView.frame to one it has after load.Or try to disable autoresizing.
Nickolay O.
Ok I did that. The only problem is that the image would have the same size as it was in portrait mode.
Oscar Del Ben