Search code examples
iphoneipaduiwebviewios7scale

scalesPageToFit not working properly running an iPhone app on iPad (iOS 7)


UIWebview scalesPageToFit is not working properly when running an iPhone app on iPad with iOS 7.

I set the scalesPageToFit = YES before loading the request to the WebView.

After the page loads, inspecting the HTML document width gives 769px while the UIWebView's scroll view frame width is 320. The scroll view zoomScale is 1 although you would expect it to be 0.41... (320/769). Any idea?


Solution

  • The problem is now fixed in iOS 7.0.3. But, if you can't go there, please read on.

    This seems to be a defect in iOS7. To recap, the problem happens when you run a iPhone only app, compiled with iOS7 SDK, in a iOS7 iPad or iPad Mini. A temporary work around is to scale the scroll view of the web view. This makes the text look smaller than you will like, but, so far, this is the best solution I have seen.

    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        CGSize contentSize = webView.scrollView.contentSize;
        CGSize viewSize = self.view.bounds.size;
    
        float scale = viewSize.width / contentSize.width;
        if (scale < 0.9) {
            NSLog(@"Zoom out fix for web view: %f", scale);
    
            webView.scrollView.minimumZoomScale = scale;
            webView.scrollView.maximumZoomScale = scale;
            webView.scrollView.zoomScale = scale;
        }
    }