UIWebView set initial zoom scale programmatically when loading an external website?

后端 未结 5 1004
日久生厌
日久生厌 2020-12-17 02:28

What I want to do is set the initial zoom scale [and in some cases, content offset, but that is less important] for an external website. But after the app initially sets th

5条回答
  •  無奈伤痛
    2020-12-17 03:12

    If you want to set initial zoom for your web view and then your web view can scaleable. You can try to add meta tag into the head tag of your HTML string at webViewDidFinishLoad protocol method. You can change 'initial-scale', 'minimum-scale'and 'maximum-scale' to adapt to your required. Looks like this:

    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        NSString* js =
        @"var meta = document.createElement('meta'); " \
        "meta.setAttribute( 'name', 'viewport' ); " \
        "meta.setAttribute( 'content', 'width = device-width, initial-scale = 1.0,minimum-scale=1.0,maximum-scale=10.0 user-scalable = yes' ); " \
        "document.getElementsByTagName('head')[0].appendChild(meta)";
        [webView stringByEvaluatingJavaScriptFromString: js];
    }
    

提交回复
热议问题