WKWebView viewport shrink-to-fit 不适用于 iOS 9.3

WKWebView viewport shrink-to-fit not working on iOS 9.3

在 iOS 9.2 中,WKWebView 呈现 HTML 固定宽度的表格大于设备宽度可以通过添加视口标签来缩小内容以适应,如下所示:

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

此行导致 WKWebView 有效地缩小视口,以便整个呈现的页面适合视图框架,而不需要滚动条。例如,考虑以下代码,当 运行 in viewDidLoad in vanilla single view app:

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 中相同的代码 运行ning 呈现如下:

(虽然截图看不到,但是有横向滚动条)

在 Whosebug 和其他地方搜索其他问题,似乎大多数其他人更喜欢 9.3 的行为。但是,我需要 9.2 行为,即收缩以适合。所以,我的问题是:有谁知道如何在 9.3 中获得相同的收缩以适应行为?有谁知道此更改是有意为之,还是将在后续版本中修复的错误?

我的测试代码可以在https://github.com/nsolter/NSWebViewShrinkTest, specifically https://github.com/nsolter/NSWebViewShrinkTest/blob/master/NSWebViewShrinkTest/ViewController.m

找到

我向 Apple 提交了错误报告,这是回复:

"No, we don’t want to maintain shrink-to-fit behavior. The workaround is to use a viewport meta tag that correctly describes the width of the content."

我试过了。如果在视口标签中,您指定 html 渲染后的实际宽度,它将在您的设备宽度上正确显示。在我上面的示例中,table 宽度为 700,如果您指定:<meta name=\"viewport\" content=\"width=700, shrink-to-fit=YES\">

然后页面将显示没有水平滚动条。

请注意,您不能指定初始比例视口 属性。然后它不会以正确的宽度显示。

这显然不太理想,因为您通常不知道渲染的 html 的宽度,直到您渲染它。我目前的解决方案是width=device-width渲染一次,然后width=X再渲染一次,其中X是第一次渲染页面的实际宽度。