2014-10-10 176 views
39

我正在更新我的iOS应用程序以用WKWebView替换UIWebView。但我不明白如何达到与WKWebView相同的行为。使用UIWebView我使用scalesPageToFit来确保网页pag以与屏幕尺寸相同的尺寸显示(以便在不滚动的情况下以全屏显示)。WKWebView相当于UIWebView的scalesPageToFit

我在网上找到了这种解决办法但它不工作:

- (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]; 
} 
+0

试试这个:'NSString * js = @“var meta = document.createElement('meta'); “ ”meta.setAttribute('name','viewport'); “ ”meta.setAttribute('content','width = device-width'); “ ”document.getElementsByTagName( '头')[0] .appendChild(元)“; [webView的stringByEvaluatingJavaScriptFromString:JS];' 上面的代码替换代码 – z22 2014-10-10 08:54:14

+0

我给它一个尝试,但我也有同样结果(顺便说一下,在WKWebView中不存在stringByEvaluatingJavaScriptFromString方法,所以我保留我的evaluateJavaScript:completionHandler调用 – olinsha 2014-10-10 15:19:41

+0

你有没有得到一个解决方案? – anoop4real 2017-05-24 05:44:34

回答

81

,你也可以尝试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添加额外的配置。

+2

作品像一个魅力感谢 – 2015-03-26 06:07:17

+1

伟大的作品 - 只有很小的调整是改变WKUserScriptInjectionTimeAtDocumentEnd WKUserScriptInjectionTimeAtDocumentStart – sckor 2015-03-31 20:54:42

+0

这应该是被接受的答案 – Brams 2015-06-17 09:45:53

7

类似@ nferocious76的,但是在斯威夫特语言

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) 
+0

谢谢!这也解决了我的问题,使html字体变大,没有从小到大刷新。我使用它来“刷新”内容字体大小和内部布局。 – 2017-07-25 14:17:13

16

nferocious76的回答,在SWIFT代码的回声: 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) 
2

C#版本,用于Xamarin自定义渲染器:

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);