Marek R
Marek R

Reputation: 37697

Adjust content scale and offset on resize of UIScrollView

I have UI which hast two states of layouts (besides portrait-landscape). In each state some other part of UI is exposed (is larger).

Problem is that UIScrollView with some UIImageView is resized on those states changes, and in each state different part of image is shown since scale and offset remains unchanged.

Is there some nice way to update this scale and offset values so more or less same part of image is shown for a large and small sized UIScrollView?

Upvotes: 0

Views: 591

Answers (1)

Charlie Price
Charlie Price

Reputation: 1376

How about the UIScrollView method

- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated

Rect is a rectangle in the coordinate space of the view returned by viewForZoomingInScrollView:.

  • Determine what rectangle of the zoomed view is being shown.
  • Change your views so that the UIScrollView bounds are changed.
  • Do the zoomToRect to show the same content, scaled as necessary.

Without having compiled and run this, it should be approximately...

CGSize rectSize;
rectSize.origin = scrollview.contentOffset;
rectSize.width = scrollview.bounds.size.width * scrollview.zoomScale;
rectSize.height = scrollview.bounds.size.height * scrollview.zoomScale;

// Do whatever makes the views change

[scrollView zoomToRect:rectSize animated:whateverYouLike];

Upvotes: 1

Related Questions