Search code examples
iphoneuiwebviewuiscrollviewzoominguiscrollviewdelegate

When I set the delegate of a UIWebView's built-in UIScrollView, I lose the ability to pinch to zoom, how can I get it back?


I am using a UIWebView to show a PDF. I need to track some UIScrollView events, so I set the built-in UIScrollView like this:

for(UIScrollView *s in webView.subviews) {

    s.delegate = self;

}

But the problem is that when I do this, I lose the ability to pinch to zoom (like I could before because the UIWebView's Scale Pages to Fit property was set). How come this happens? What can I do to fix it? I went ahead and implemented the UIScrollView shouldZoom method, but when I pass back the scrollView as the view to zoom on, it zooms from the corner, not where my fingers are.

Does anybody know a solution to this problem? How can I have the UIScrollView delegate set, and still retain natural zooming ability?

Thanks!


Solution

  • Either handle the following events in your delegate:

    – viewForZoomingInScrollView:
    – scrollViewWillBeginZooming:withView:
    – scrollViewDidEndZooming:withView:atScale:
    – scrollViewDidZoom:
    

    or save the original delegate

    origDelegate = s.delegate;
    s.delegate = self;
    

    and forward the calls, e.g:

    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
        return [origDelegate viewForZoomingInScrollView:scrollView]; 
    }