ios-WKWebView等效于UIWebView的scalesPageToFi

我正在更新我的iOS应用,以将UIWebView替换为WKWebView。但是,我不明白如何使用WKWebView来实现相同的行为。对于UIWebView,我使用了scalesPageToFit来确保以与屏幕大小相同的大小显示Web页(例如 以全屏显示而不滚动)。

我在网上发现了该解决方案,但是它不起作用:

- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
NSString *javascript = @"var meta = document.createElement('meta');meta.setAttribute('name', 'viewport');meta.setAttribute('content', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no');document.getElementsByTagName('head')[0].appendChild(meta);";
[webView evaluateJavaScript:javascript completionHandler:nil];
}
olinsha asked 2019-10-09T04:48:12Z
5个解决方案
96 votes

您也可以尝试WKUserScript。

这是我的工作配置:

NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";

WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *wkUController = [[WKUserContentController alloc] init];
[wkUController addUserScript:wkUScript];

WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
wkWebConfig.userContentController = wkUController;

wkWebV = [[WKWebView alloc] initWithFrame:self.view.frame configuration:wkWebConfig];

您可以将其他配置添加到WKWebViewConfiguration。

nferocious76 answered 2019-10-09T04:48:38Z
33 votes

nferocious76的答案的回声,采用快速代码:Swift2.x版本

let jscript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"
let userScript = WKUserScript(source: jscript, injectionTime: WKUserScriptInjectionTime.AtDocumentEnd, forMainFrameOnly: true)
let wkUController = WKUserContentController()
wkUController.addUserScript(userScript)
let wkWebConfig = WKWebViewConfiguration()
wkWebConfig.userContentController = wkUController
let youWebView = WKWebView(frame: CGRectZero, configuration: wkWebConfig)

swift3版本

let jscript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"
let userScript = WKUserScript(source: jscript, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
let wkUController = WKUserContentController()
wkUController.addUserScript(userScript)
let wkWebConfig = WKWebViewConfiguration()
wkWebConfig.userContentController = wkUController
let yourWebView = WKWebView(frame: self.view.bounds, configuration: wkWebConfig)
air_bob answered 2019-10-09T04:49:10Z
11 votes

与@ nferocious76相似,但使用Swift语言

var scriptContent = "var meta = document.createElement('meta');"
scriptContent += "meta.name='viewport';"
scriptContent += "meta.content='width=device-width';"
scriptContent += "document.getElementsByTagName('head')[0].appendChild(meta);"

webView.evaluateJavaScript(scriptContent, completionHandler: nil)
Efe Ariaroo answered 2019-10-09T04:49:35Z
5 votes

C#版本,用于Xamarin Custom Renderer:

string jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";

WKUserScript wkUScript = new WKUserScript((NSString)jScript, WKUserScriptInjectionTime.AtDocumentEnd, true);
WKUserContentController wkUController = new WKUserContentController();
wkUController.AddUserScript(wkUScript);

WKWebViewConfiguration wkWebConfig = new WKWebViewConfiguration();
wkWebConfig.UserContentController = wkUController;
WKWebView webView=new WKWebView(Frame, wkWebConfig);
Fire in the Hole answered 2019-10-09T04:50:00Z
0 votes

添加元标记或更改Java脚本代码对我不起作用。 然后我更改了框架,使其工作良好,在编写以下代码(Swift)之后,我的网络视图对齐并适合屏幕:

var screenWidth: CGFloat {
    return UIScreen.main.bounds.size.width
}

var screenHeight: CGFloat {
    return UIScreen.main.bounds.size.height
}


    yourWebView = WKWebView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight), configuration: WKWebViewConfiguration())
    yourWebView.navigationDelegate = self
    yourContainerView.addSubview(yourWebView)
Burcu K. Kutluay answered 2019-10-09T04:50:25Z
translate from https://stackoverflow.com:/questions/26295277/wkwebview-equivalent-for-uiwebviews-scalespagetofit