How change min and max zoom scale in WKWebView?

2019-04-15 03:48发布

I use WKWebView to show svg. It's rather big and i need support of scaling it on pinch gesture. WKWebView do it out of the box. But. It has it's own constraints for min and max zoom scale. For example:

subscribe to WKWebView's scrollView delegate and implement method

optional func scrollViewDidEndZooming(_ scrollView: UIScrollView, 
                                 with view: UIView?, 
                              atScale scale: CGFloat)
{
        print(self.wkWebView.scrollView.minimumZoomScale, self.wkWebView.scrollView.maximumZoomScale)
        self.wkWebView.scrollView.minimumZoomScale = 0.01
        self.wkWebView.scrollView.maximumZoomScale = 20.0
}

And in next call it will print: 0.25, 5.0. My values of (0.01, 20.0) are absolutely ignored. Is it a way to correct it?

With deprecated UIWebView there was no such problem. (But there was very unpredictable scaling.)

1条回答
倾城 Initia
2楼-- · 2019-04-15 04:29

I know this question is old now but I'm tackling what seems to be a similar problem and thought my solution might help others. I've wrapped the SVG in the following HTML:

<html>
    <head>
        <meta id="mw-viewport" name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1, initial-scale=1.0"/>
    </head>
    <body>
        <svg/>
    </body>
</html>

Once I've calculated my desired min/max zoom levels I execute some javascript to grab the "mw-viewport" meta tag and edit it's "content" attribute's "minimum-scale=1, maximum-scale=1" values.

查看更多
登录 后发表回答