UIImage does not scroll in iPhone
ipad, iphone, uiimage, uiscrollview
Solution
the answer is actually a combination of many of the other answers & comments, plus some other adjustments.
- 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.
- 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.
- 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.
- as Kunal Balani mentioned, you have to have scrollingEnabled, though you can set this in storyboard or interface builder.
- 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.
- 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.
- 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.
- 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;
}
Problem
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.