4

I am trying to load HTML content to WKWebView, It does not load properly the same as screen height/width, same code is working fine with UIWebView, See the following code

webView.loadHTMLString(htmlString!, baseURL: nil)

HTML string content <style type="text/css"> #container { width: 667px; height: 375px; margin: 0 auto } </style> 667px is screen height and 375px is screen width.

<!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>

What can I do to solve this issue?

enter image description here

2
  • try to change webView contentMode, it should resize its content Commented Jul 25, 2019 at 11:01
  • I have tried contentMode with AspectFill, AspectFit, scale to Fill but not getting proper output. Commented Jul 25, 2019 at 11:10

1 Answer 1

11

Add headerString before your html code and try it Like below

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @ishwarLalJanwa, It's working but, If I set leading and trailing spaces 30 pixels between WKWebView and main view then it will show horizontally scroll, I don't want to scroll then what can I do for this.
How can I fit chart exactly to WKWebView (same as WKWebView size)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.