美文网首页
WKWebView中实现scalesPageToFit

WKWebView中实现scalesPageToFit

作者: 学学学q | 来源:发表于2021-03-30 14:23 被阅读0次

    使用WKWebView时,发下没有UIWebView中的scalesPageToFit属性
    解决方法:

    NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
    
    WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    WKUserContentController *wkUController = [[WKUserContentController alloc] init];
    [wkUController addUserScript:wkUScript];
    
    WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
    wkWebConfig.userContentController = wkUController;
    
    wkWebV = [[WKWebView alloc] initWithFrame:self.view.frame configuration:wkWebConfig];
    

    或者在didFinishNavigation中执行:

        NSString *jscript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"
        [webView evaluateJavaScript:jscript completionHandler:nil];
    

    或者添加以下代码

    <meta name="viewport" content="width=device-width, shrink-to-fit=YES">
    

    相关文章

      网友评论

          本文标题:WKWebView中实现scalesPageToFit

          本文链接:https://www.haomeiwen.com/subject/mvfghltx.html