1
votes

I have a UIScrollView with a UIImageView subview created in IB. The first time I create a UIImage and set it to be the image in the UIImageView all works fine. If I create a new UIImage and set it to be the image in the UIImageView the image is offset by some (seemingly) random offset.

How can I make this work?

2
It seems that it is necessary to create a new UIScrollView and UIImageView programmatically, at least this works. - zaph

2 Answers

2
votes

It seems that this should be as easy as setting a new image but it seems that as a minimum the following is necessary:

imageScrollView.transform = CGAffineTransformIdentity;
imageScrollView.contentOffset = CGPointZero;
imageScrollView.contentSize = self.imageScrollView.frame.size;

[imageScrollView removeFromSuperview];
imageView = [[TapDetectingImageView alloc] initWithImage:newImage];

[imageScrollView addSubview: imageView];

Where imageScrollView, imageScrollView and newImage are previously instantiated.

1
votes

Does the second image have a different aspect ratio than the first? If so, look at UIImageView's contentMode property.