0
votes

I'm working on an app. Here I describe a simplified version of a strange problem I am experiencing.

I have a storyboard created View Controller with a UIImageView also created in the storyboard.

I then have the following h and m files which aim to add a UIScrollView to the UIImageview and give the scrollview a container.

h file:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *imageView;

@property (nonatomic, strong) UIScrollView* scrollView;
@property (nonatomic, strong) UIView* container;

@end

m file:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *image = [UIImage imageNamed: @"new_phots_fake_port.png"];
    //Add the image to the imageView I created in the Storyboard
    [_imageView setImage:image];


    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(60.0, 70.0, 522.0, 100.0)];
    [_scrollView setBackgroundColor:[UIColor yellowColor]];


    // Set up the container view to exist inside the scrollview:
    CGSize containerSize = CGSizeMake(5000.0f, 730.0f);
    _container = [[UIView alloc] initWithFrame:(CGRect){.origin=CGPointMake(0.0f, 0.0f),       .size=containerSize}];
    _container.backgroundColor = [UIColor redColor];
    // Tell the scroll view the size of the contents
    self.scrollView.contentSize = containerSize;
    [_scrollView addSubview:_container];


    [_imageView addSubview:_scrollView]; //This is what I want but doesn't allow scrolling

    //[self.view addSubview:_scrollView];  //This allows scrolling but makes the imageView      and Scrollview siblings.  I need scrollview to be a child of imageview
   }

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

@end

So, the scrollview cannot seem to be added to the imageview (and function correctly). Why? What am I doing wrong?

Strangely, I also didn't use either of the delegate methods viewForZoomingInScrollView or scrollViewDidZoom. I'm not sure why these were not needed.

1

1 Answers

1
votes

You need to set userInteractionEnabled on the UIImageView

_imageView.userInteractionEnabled = YES;