简体   繁体   中英

Programmatically zoom a UIWebView by addressing its UIScrollView

in my xib-based view I have a UIWebView that I need to zoom programmatically.

Since the UIWebView has no own zooming methods I found Max's post that suggested to get the UIWebViews UIScrollView and send the zoom levels to that one.

In my view controller's viewDidLoad method I therefore determined the UIScrollView as follows:

- (void)viewDidLoad {
   // set iVars (both are IBOutlets)
   iWebView.delegate = self;
   iScrollView = [[iWebView subviews] objectAtIndex:0];

   iScrollview.minimumZoomScale = 0.2;
   iScrollview.maximumZoomScale = 4.0;
   iScrollview.zoomScale = iScrollview.minimumZoomScale;

NSLog(@"webViewDidFinishLoad: Current %.3f, Min %.3f, Max %.3f", 
          iMapScrollView.zoomScale, iMapScrollView.minimumZoomScale, iMapScrollView.maximumZoomScale);
   ...
}

The UIWebView (by default) contains a 1000x2000px png-file. The NSLog shows

webViewDidFinishLoad: Current 0.200, Min 0.200, Max 4.000

Trouble comes when I try to update the iScrollView.zoomLevel to a new zoom-factor (lets say to 2.4):

- (void)setPositionWithZoom:(float)xPos down:(float)yPos zoomScale:(float)mZoomScale {
   iScrollView.zoomScale = mZoomScale;
   NSLog(@"setPositionWithZoom: mZoomScale %.3f Current %.3f, Min %.3f, Max %.3f", 
           mZoomScale , iMapScrollView.zoomScale, iMapScrollView.minimumZoomScale, iMapScrollView.maximumZoomScale);
   ...
}

The NSLog shows:

setPositionWithZoom: mZoomScale 2.400 mCurrent 1.000 Min 1.000 Max 1.000

So the zoom-level resets to 1 since the min/max zoomlevels have mysteriously fallen back to that value.

I'm flabbergasted, any clues?


EDIT: Further investigation brought me to a touch method that I call from a UIWindow-subclass to forward touch events (implementation of Mithins recipe for grasping touch events with UIWebViews).

It is when this method ( myWebViewTouchesEnded ) in my viewController gets called that the values are wiped out. I have no idea why.

PATCH: I applied a simple patch to define 2 iVars that i use to set the min/max zoomScales back to my custom values within myWebViewTouchesEnded and evrything works ...

Did you mean iScrollView instead of iMapScrollView in you NSLog statement?

Be aware that UIWebView does flakey things with zoomScale, minimumZoomScale and maximumZoomScale.

For example:

When loading a file with scalesPageToFit: if file is a PDF, min... = 1 max... = 32 and zoomScale =1, When you zoom, zoomScale changes appropriately and min... and max... remain unchanged.

if the file is an Excel file, min...=1 max... = some number N and zoomScale =1 When you zoom, zoomScale remains = 1, while max... and min... change, eg set zoomScale =2 and zoomScale will = 1, min... will = 1/2 and max... will = N/2

EDIT: This problem with Excel files applies only when you have zoomed with a pinch gesture.

Try this:

for (UIScrollView *scroll in [myPDFView subviews])
{   
    //Set the zoom level.

    [scroll setZoomScale:2.5f animated:YES];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM