The zoom for uiscrollview isn't working

2019-03-04 06:04发布

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.

3条回答
smile是对你的礼貌
2楼-- · 2019-03-04 06:11

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

[scrollView addSubview:imgv];
查看更多
在下西门庆
3楼-- · 2019-03-04 06:32

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);
查看更多
贪生不怕死
4楼-- · 2019-03-04 06:33

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

- (UIView *) viewForZoomingInScrollView:(UIScrollView *) view {
    return imgv;
}
查看更多
登录 后发表回答