The zoom for uiscrollview isn't working

2019-03-04 05:53发布

问题:

I don't understand why the scrollview isn't working, because I followed advices from tutorials.

I created the UIScrollView that contains an UIImageView and I also added the method

 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
 {
   return imgv;
 }

and the UIScrollViewDelegate in the interface, but it isn't working.

Here is the code for the UIScrollView:

 imgv = [[UIImageView alloc]
                     initWithFrame:CGRectMake(0, 0, 1000, 500)];
 imgv.image = [UIImage imageWithData:retrievedData];

 scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 800, 800)];
 scrollView setContentSize:CGSizeMake(imgv.image.size.width, imgv.image.size.height)];
 scrollView.maximumZoomScale = 4;
 scrollView.minimumZoomScale = 1;
 [scrollView setScrollEnabled:YES];
 scrollView.clipsToBounds = YES;
 scrollView.delegate = self;

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(hideImageView)];

 [scrollView addSubview:imgv];
 [view addSubview:scrollView];

I would be thankful, if someone could figure out what the problem could be.

回答1:

You need to return imgv in your delegate method viewForZoomingInScrollView::

- (UIView *) viewForZoomingInScrollView:(UIScrollView *) view {
    return imgv;
}


回答2:

You have to add the imageView as a subview to the scrollview

[scrollView addSubview:imgv];


回答3:

I believe you are forgetting to set the frame of your imageView properly

self.imageView.frame=CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);