在 iOS 9.2 中,可以告诉 WKWebView 呈现 HTML 的固定宽度表格大于设备宽度,可以通过添加如下视口标签来缩小内容以适应:
<meta name="viewport" content="width=device-width, shrink-to-fit=YES">
此行导致 WKWebView 有效地缩小视口,以便整个渲染页面适合视图框架,而无需滚动条。例如,考虑以下代码,当在 vanilla 单视图应用程序的 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 中运行的相同代码呈现如下:
(虽然在截图中看不到,但是有一个水平滚动条)
在 stackoverflow 和其他地方搜索其他问题,似乎大多数其他人更喜欢 9.3 的行为。但是,我需要 9.2 的收缩以适应的行为。所以,我的问题是:有谁知道如何在 9.3 中获得相同的收缩以适应行为?有谁知道这种变化是故意的,还是将在后续版本中修复的错误?
我的测试代码可以在https://github.com/nsolter/NSWebViewShrinkTest找到,特别是https://github.com/nsolter/NSWebViewShrinkTest/blob/master/NSWebViewShrinkTest/ViewController.m