I want to allow a user to associate a zoom factor with a document and use it as a starting point when displaying that document inside a UIWebView. It seems, however, that webViewDidFinishLoad: only indicates the end of in-memory loading, not including rendering or layout. Here’s sample code to demonstrate the problem:
- (void)viewDidLoad {
[super viewDidLoad];
UIWebView *webView = (UIWebView *)self.view;
webView.delegate = self;
webView.scalesPageToFit = YES;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSString *urlString = [[NSBundle mainBundle] pathForResource:@"Doc" ofType:@"pdf"];
NSURL *file = [NSURL fileURLWithPath:urlString];
[(UIWebView *)self.view loadRequest:[NSURLRequest requestWithURL:file]];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (!webView.isLoading) {
[webView.scrollView setZoomScale:1.5 animated:YES];
}
}
The call to setZoomScale: executes with no effect (i.e. the file is displayed with a zoom factor of 1.0), apparently because it happens before the scroll view is in some state where it can deal with it. If I change the final method above to what follows, everything works as I’d hoped.
- (void)delayedZoomMethod {
[((UIWebView *)self.view).scrollView setZoomScale:1.5 animated:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self performSelector:@selector(delayedZoomMethod) withObject:nil afterDelay:1.0];
}
This, of course, is a bad idea because the 1.0 delay is arbitrary, probably too long for the vast majority of cases, and will likely be too short under some unknown set of conditions.
The docs say, “Your application can access the scroll view if it wants to customize the scrolling behavior of the web view.” Does anyone know of a notification, or property in the web view or its scroll view, that I could observe to be told when that statement becomes true?
If you have control of the html code this may help. I’ve been able to have html events trigger objC methods in the following manner.
html event triggers javascript that writes a string to window.location
the uiwebview then uses uiwebviewdelegate to receive the string with this function
webView:shouldStartLoadWithRequest:navigationType:
It’s kind of hacky.
But I don’t think there is a way to know the web view has finished rendering, unless you wrote the html page and can put a js function in to execute at the end of the page.