在iOS 9.2中,可以通过添加如下视口标记来告知WKWebView呈现具有固定宽度表大于设备宽度的HTML的HTML:WKWebView视口缩小到适合在iOS上不起作用9.3
<meta name="viewport" content="width=device-width, shrink-to-fit=YES">
此行导致WKWebView有效缩小视口,以便整个呈现的页面无需滚动条即可适应视图框架。例如,请考虑下面的代码,在viewDidLoad中的香草单一视图的应用程序运行时:
WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
WKWebView *newWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0,40,self.view.frame.size.width, self.view.frame.size.height - 40) configuration:wkWebConfig];
NSString *toRender = @"<head><meta name=\"viewport\" content=\"width=device-width, shrink-to-fit=YES\"></head><body><table width=700 style='background-color: blue; color:white; font-size=20px'><tr><td>this is some text that is long enough to exceed the width of the iphone 6 unless shrink-to-fit is applied</td></tr></table></body>";
[newWebView loadHTMLString:toRender baseURL:nil];
[self.view addSubview:newWebView];
内容呈现这样的:
然而,这种行为在9.3改变。在9.3中运行的相同代码将呈现这样的:
(虽然你不能看到它的屏幕截图,有一个水平滚动条)
搜索计算器上的其他问题和其他地方,它似乎大多数其他人更喜欢9.3的行为。但是,我需要9.2缩小到适合的行为。所以,我的问题是:有没有人知道如何在9.3中获得相同的缩小到适合的行为?有没有人知道这种改变是有意的,还是一个将在后续版本中修复的错误?
我的测试代码可以在https://github.com/nsolter/NSWebViewShrinkTest发现,特别是https://github.com/nsolter/NSWebViewShrinkTest/blob/master/NSWebViewShrinkTest/ViewController.m
我也使用宽度=设备宽度和面临同样的问题。你正在重新加载宽度= X的页面? – anoop4real
基本上,是的。我实际上是将它呈现在当前未显示的新WKWebView中,然后在新的webview中进行交换。 –
你有样品吗? – anoop4real