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 8316337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:20:25+00:00 2026-06-08T21:20:25+00:00

I am new to iPhone developer, I want scrolling in my Image, Here is

  • 0

I am new to iPhone developer,

I want scrolling in my Image,

Here is my code snippet,

- (void)viewDidLoad {
UIImage *image1 = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bg.png"]];
    self.imageView = [[UIImageView alloc] initWithImage:image1];
    self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image1.size};
    [self.scrollView addSubview:self.imageView];
    self.scrollView.contentSize = image.size;

 [self centerScrollViewContents];
}


- (void)centerScrollViewContents {
    CGSize boundsSize = self.scrollView.bounds.size;
    CGRect contentsFrame = self.imageView.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;
    }

    self.imageView.frame = contentsFrame;
}

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    // Return the view that you want to zoom
    return self.imageView;
}

also bg.png is not getting applied to my background.

Any help will be appreciated.

  • 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-08T21:20:27+00:00Added an answer on June 8, 2026 at 9:20 pm

    the answer is actually a combination of many of the other answers & comments, plus some other adjustments.

    1. as Pan said, you need to set your scrollView delegate. otherwise, your
      viewForZoomingInScrollView: delegate implementation won’t get called.
      i’ve provided this call in viewDidLoad: , though you can also hook this
      up in interface builder or storyboard.
    2. as Teodor Carstea said in the comments to the original post, in your
      original code, you are setting your .imageView.size and your
      .scrollView.contentsSize to exactly the same thing. you don’t need
      the code that sets the .imageView.frame. that will happen as part of
      initWithImage: . and this will make the sizes different.
    3. you do need to set the .scrollView.contentSize to the image.size .
      it sort of matters where ou do this, at least in terms of that it must
      happen prior to setting the zoom scale.
    4. as Kunal Balani mentioned, you have to have scrollingEnabled, though you
      can set this in storyboard or interface builder.
    5. as MidhunMP said, you have to have .userInteractionEnabled = YES, though
      generally, these are the defaults that get set in storyboard/Interface Builder,
      so i’m not going to include them in the code changes below.
    6. your method centerScrollViewContents isn’t really centering the contents
      as much as it is re-shaping the imageView frame. i’ve simply commented out
      the call, because i think it’s completely unnecessary. instead, i’ve made
      a simple call to start your image in the upper left corner.
    7. if you only want to scroll, setting the zoomScale, minimumZoomScale and
      maximumZoomScale is not necessary. if you also want to pinch-zoom, then
      you will need to set all three of these, and in the proper places.
    8. you do seem to want to stretch the view if it is smaller than the content
      bounds. if this is truly a concern (i.e. if you know your bg.png is so
      small that it needs to be stretched), then calculate the contentFrame to
      image frame width ratio and height ratio separately, and whichever of
      these is larger will be your new zoomScale, and you’ll be able to scroll
      in the other direction. or, there are other ways to decide how you want
      that scale set. search stackoverflow.com for other answers for setting
      the zoom scale as desired.

    here’s the code after applying these items.

    (void)viewDidLoad {
        self.scrollView.delegate = self;         // can be set in storyboard
        self.scrollView.scrollingEnabled = YES;  // can be set in storyboard
        NSString* bgPng
          = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bg.png"];
        UIImage *image1 = [UIImage imageWithContentsOfFile:bgPng];
        self.imageView = [[UIImageView alloc] initWithImage:image1];
    //  self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image1.size};
        [self.scrollView addSubview:self.imageView];
        self.scrollView.contentSize = image.size;
    
        // set zoom scale here if desired.
    
        // if zoom is implemented, the following call should be made
        // after setting .zoomScale, .minimumZoomScale & .maximumZoomScale
        self.scrollView.contentOffset = CGPointZero;
    
    //  [self centerScrollViewContents];
    }
    
    
    //- (void)centerScrollViewContents {
    //    CGSize boundsSize = self.scrollView.bounds.size;
    //    CGRect contentsFrame = self.imageView.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;
    //    }
    //  
    //    self.imageView.frame = contentsFrame;
    //}
    
    - (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
        // Return the view that you want to zoom
        return self.imageView;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to iPhone developer, I want to implement different Animations on button
I am new to iPhone Developer. i want to do navigate to new page
I am new to iPhone Developer, I want to detect touch in my webview
I am new to iPhone developer, On click of button, i want to navigate
fairly new iPhone developer here. Building an app to send RS232 commands to a
im new developer and making my firt iPhone app ,and i want to make
I'm a new iphone developer, question is I want to know the sym.data 's
I am new in iPhone Developer so I want learn SQLite Database from begin
I am a new iPhone Application developer. I want to create an application which
i am a new beginner of iphone developer and i want to compare my

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.