Thomas Riley
Thomas Riley

Reputation: 409

Can't get a UIScrollView to zoom and scroll

I know this has been posted about a million times, but I cannot for the life of me get this Scrollview to work how it should. I have followed about four different tutorials and have spent the last two days attempting to fix this, so I really do hope someone can help me through this (if there happens to be another post that fixes my problem that I didn't see, I'm really sorry).

I have an Imageview inside of a Scrollview, containing a picture I'd like to be zoomable/scrollable when it's zoomed.

In my WDViewController.h file:

#import <UIKit/UIKit.h>

@interface WDViewController : UIViewController <UIScrollViewDelegate>
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UIImageView *img_wd;

@end

And in my WDViewController.m file:

#import "WDViewController.h"

@implementation WDViewController

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.img_wd;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.scrollView.minimumZoomScale=0.5;
    self.scrollView.maximumZoomScale=6.0;
    self.scrollView.delegate=self;
    self.scrollView.contentSize=CGSizeMake(1280, 960);
    self.scrollView.scrollEnabled = true;
    self.scrollView.userInteractionEnabled = true;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Autolayout is disabled, as I noticed that caused problems to other users on Stack Overflow. I made sure that User Interaction and Multiple Touch were enabled in the Storyboard. But when I launch the app in a simulator, it just sits there. No matter how many times I pinch it doesn't zoom. I thought I did everything, but I must have missed a step that I'm just not seeing for some reason. Can anyone give me a hand?

EDIT: After the comments below, here's the code I have now (for WDViewController.m, .h hasn't changed):

#import "WDViewController.h"

@implementation WDViewController

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.img_wd;
}

- (void)viewDidLoad {
     [super viewDidLoad];
    [self.scrollView addSubview:self.img_wd];
     self.img_wd.frame = CGRectMake(0, 0, 1280, 960);
     self.scrollView.minimumZoomScale=0.5;
     self.scrollView.maximumZoomScale=6.0;
     self.scrollView.delegate=self;
    self.scrollView.contentSize=CGSizeMake(1280, 960);
     self.scrollView.scrollEnabled = true;
     self.scrollView.userInteractionEnabled = true;
    }

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

EDIT: And after testing it on my phone, it works. Um... okay then, thanks everyone. My simulator just refuses to accept it. How odd.

Upvotes: 1

Views: 2458

Answers (3)

bilobatum
bilobatum

Reputation: 8918

View hierarchy in IB:

View hierarchy in IB

The scroll view was dragged and dropped onto the root view and was snapped into place to fill the entire root view. Image view was dragged and dropped onto the scroll view and was snapped into place to fill the entire scroll view.

Attributes for image view in IB:

enter image description here

All source code:

enter image description here

Upvotes: 2

bilobatum
bilobatum

Reputation: 8918

Just a hunch:

To pinch in the iOS Simulator, you have to hold down the option key.

Upvotes: 3

KudoCC
KudoCC

Reputation: 6952

did you add the image view to scrollview's subview? [self.scrollView addSubView:self.img_wd]

Upvotes: 1

Related Questions