The zoom for uiscrollview isn't working

主宰稳场 提交于 2019-12-31 05:14:07

问题


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);


来源:https://stackoverflow.com/questions/13567887/the-zoom-for-uiscrollview-isnt-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!