Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8506837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:42:06+00:00 2026-06-11T02:42:06+00:00

I want help with my UIScrollView sample. I created a simple program that scrolls

  • 0

I want help with my UIScrollView sample.

I created a simple program that scrolls and zooms the content (UIImageView). It works fine, except that the content frequently disappears to the right-bottom when I try zooming out. But since I set minimumZoomScale to 1.0f, it is actually not zooming out, only the content is jumping out of the view. And what is even more weird is that I cannot scroll up after this. Apparently content size is messed up as well.

enter image description here

The setup I have in my sample code is as in the figure below.

enter image description here

When I checked the status after (trying) zooming out, I found two wrong things.

  • _scrollView.contentSize is 480×360, which should not be smaller than 1000×1000
  • _scrollView.bounds jumped to the top somehow (i.e., _scrollView.bounds.origin.y is always 0)

To cope with the two items above, I added following code in my UIScrollViewDelegate and now it works fine.

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
    if(scrollView == _scrollView && view == _contentView)
    {
        // Setting ivars for scrollViewDidZoom
        _contentOffsetBeforeZoom = _scrollView.contentOffset;
        _scrollViewBoundsBeforeZoom = _scrollView.bounds;
    }
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    if(scrollView == _scrollView)
    {
        // If you zoom out, there are cases where ScrollView content size becomes smaller than original,
        // even though minimum zoom scale = 1. In that case, it will mess with the contentOffset as well.
        if(_scrollView.contentSize.width < CONTENT_WIDTH || _scrollView.contentSize.height < CONTENT_HEIGHT)
        {
            _scrollView.contentSize = CGSizeMake(CONTENT_WIDTH, CONTENT_HEIGHT);
            _scrollView.contentOffset = _contentOffsetBeforeZoom;
        }

        // If you zoom out, there are cases where ScrollView bounds goes outsize of contentSize rectangle.
        if(_scrollView.bounds.origin.x + _scrollView.bounds.size.width > _scrollView.contentSize.width ||
           _scrollView.bounds.origin.y + _scrollView.bounds.size.height > _scrollView.contentSize.height)
        {
            _scrollView.bounds = _scrollViewBoundsBeforeZoom;
        }            
    }
}

However, does it need to come down to this? This is a very simple sequence, and it is hard to believe that Apple requires us to put this kind of effort. So, my bet is I am missing something here…

Following is my original code. Please help me find what I am doing wrong (or missing something)!

#define CONTENT_WIDTH 1000
#define CONTENT_HEIGHT 1000

  >>>> Snip >>>>

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

    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    _scrollView.contentSize = CGSizeMake(CONTENT_WIDTH, CONTENT_HEIGHT);
    _scrollView.backgroundColor = [UIColor blueColor];
    _scrollView.maximumZoomScale = 8.0f;
    _scrollView.minimumZoomScale = 1.0f;
    _scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    _scrollView.scrollEnabled = YES;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];

    _contentView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sample.jpg"]]; // sample.jpg is 480x360
    CGPoint center = (CGPoint){_scrollView.contentSize.width / 2, _scrollView.contentSize.height / 2};
    _contentView.center = center;
    [_scrollView addSubview:_contentView];

    _scrollView.contentOffset = (CGPoint) {center.x - _scrollView.bounds.size.width / 2, center.y - _scrollView.bounds.size.height / 2};        
}

- (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    if(scrollView == _scrollView)
    {
        return _contentView;
    }
    return nil;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:42:08+00:00Added an answer on June 11, 2026 at 2:42 am

    I created a quick sample project and had the same issue you described using the code you pasted. I don’t exactly know what the “proper” way to zoom is in iOS but I found this tutorial which says that you need to recenter your contentView after the scrollView has been zoomed. I would personally expect it to be automatically re-centered given that it is the view you’re returning in the viewForZoomingInScrollView delegate method but apparently not.

    - (void)centerScrollViewContents {
        CGSize boundsSize = _scrollView.bounds.size;
        CGRect contentsFrame = _contentView.frame;
    
        if (contentsFrame.size.width < boundsSize.width) {
            contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
        } else {
            contentsFrame.origin.x = 0.0f;
        }
    
        if (contentsFrame.size.height < boundsSize.height) {
            contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
        } else {
            contentsFrame.origin.y = 0.0f;
        }
    
        _contentView.frame = contentsFrame;
    }
    
    - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
        // The scroll view has zoomed, so we need to re-center the contents
        [self centerScrollViewContents];
    }
    

    The code above is not written by me but is simply copied from the tutorial. I think its pretty straightforward. Also, centring the contentView seems to be a lot more elegant then constantly changing the bounds and content size of the scrollview so give it a try.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created sample circular UIScrollview.it works fine.but i want to scroll through NSTimer
I want a program that would help me create virtual devices on a virtual
I want to implement a simple help form to appear once the application has
i want to display image in UIImageView which is on the UIScrollView and using
I want help on this script I am making... I want my website to
hey i guys i want help from you. i am working on website project,
I am in a learning way of mysql and want help to know where
I want to help user to take their pictures with a plugged in video
I'm a final year high school student and want to help my father secure
I want to show some help text(much like in a webpage when a field

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.