UIWebView does not scale content to fit

前端 未结 11 870
忘了有多久
忘了有多久 2020-11-29 00:21

I have a webview which is the top window in the hierarchy and has been declared as shown below. However, it does not scale pages to fit. Pages are top left aligned, but are

相关标签:
11条回答
  • 2020-11-29 00:48

    In xCode 8.3.3, go to the storyboard of the view/scene where the web view is located and check Scale Page to Fit in the attributes inspector section . Also select Scale to Fill as the content mode.

    0 讨论(0)
  • 2020-11-29 00:51

    I have subsequently discovered that some web pages are correctly scaled, but some are not. For web pages which are not properly scaled, javascript can be used to control zooming as follows:

    NSString *jsCommand = [NSString stringWithFormat:@"document.body.style.zoom = 1.5;"];
    [webLookupView stringByEvaluatingJavaScriptFromString:jsCommand];
    
    0 讨论(0)
  • 2020-11-29 00:51

    in Swift2

    self.webView.scalesPageToFit = true
    self.webView.contentMode = UIViewContentMode.ScaleAspectFit
    
    0 讨论(0)
  • 2020-11-29 00:53

    There is "Scale To Fit" option in interface builder, so it should be something programatic to do the same.

    0 讨论(0)
  • 2020-11-29 00:54

    You can use the below code:

    self.webView.scalesPageToFit = YES;
    self.webView.contentMode = UIViewContentModeScaleAspectFit;
    

    in - (void)webViewDidFinishLoad:(UIWebView *)webView method or viewDidLoad

    0 讨论(0)
  • 2020-11-29 00:55

    iOS5 gives a better solution

    call this from webViewDidFinishLoad

    - (void)zoomToFit
    { 
        if ([theWebView respondsToSelector:@selector(scrollView)])
        {
            UIScrollView *scrollView = [theWebView scrollView];
    
            float zoom = theWebView.bounds.size.width / scrollView.contentSize.width;
            scrollView.minimumZoomScale = zoom;
            [scrollView setZoomScale:zoom animated:YES];
        }
    }
    
    0 讨论(0)
提交回复
热议问题