WKWebView的“ loadHTMLString”高度宽度问题

时间:2019-07-25 10:19:12

标签: ios swift uiwebview wkwebview wkwebviewconfiguration

我正在尝试将HTML内容加载到WKWebView,不能正确加载与屏幕高度/宽度相同的内容,使用UIWebView可以正常运行相同的代码,请参见以下代码

webView.loadHTMLString(htmlString!, baseURL: nil)

HTML字符串内容<style type="text/css"> #container { width: 667px; height: 375px; margin: 0 auto } </style>的屏幕高度是667px,屏幕的宽度是375px。

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>Highcharts Example</title>

            <style type="text/css">
                #container {
                    width: 667px;
                    height: 375px;
                    margin: 0 auto
                }
            </style>
            </head>
    <body>
        <script src="https://code.highcharts.com/highcharts.js"></script>

        <div id="container"></div>


        <script type="text/javascript">

            Highcharts.chart('container', {   
                    //Chart code.
                             });
            </script>
    </body>
</html>

我该怎么做才能解决此问题?

enter image description here

1 个答案:

答案 0 :(得分:0)

在您的html代码之前添加headerString,然后像下面一样尝试

let description = "<p> HTML content <p>"
var headerString = "<header><meta name='viewport' content='width=device-width, 
initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0'></header>"
headerString.append(description)              
self.webView.loadHTMLString("\(headerString)", baseURL: nil)