UIWebView — load external website, programmatically set initial zoom scale, and allow user to zoom afterwards

前端 未结 5 1179
忘了有多久
忘了有多久 2020-12-02 23:58

Is it possible to do all of the above? SO has given me a great way to set the initial zoom scale here. Namely, to include the following line in my webViewDidFinishLoad met

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 00:38

    The difference with my answer is you are clearing the viewport before setting the width.

    This allows for the web view to be rotated or resized (after being loaded) without resetting your attribute changes.

    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
        // Clear view port
        NSString* js = @"$('meta[name=viewport]').remove();";
        [webView stringByEvaluatingJavaScriptFromString: js];
    
        // Set content size
        js = [NSString stringWithFormat:@"var meta = document.createElement('meta');"
              "meta.setAttribute( 'name', 'viewport' ); "
              "meta.setAttribute( 'content', 'width = device-width, initial-scale = 5.0, user-scalable = yes' ); "
              "document.getElementsByTagName('head')[0].appendChild(meta);"];
    
        [webView stringByEvaluatingJavaScriptFromString: js];
    }
    

提交回复
热议问题