2014-10-10 14 views
39

Aktualizuję moją aplikację na iOS, aby zastąpić UIWebView z WKWebView. Jednak nie rozumiem, jak osiągnąć to samo zachowanie z WKWebView. Z UIWebView użyłem scalesPageToFit, aby zapewnić wyświetlanie strony internetowej o rozmiarze takim samym jak rozmiar ekranu (aby wyświetlać cały ekran bez przewijania).Ekwiwalent WKWebView dla wag UIWebViewPageToFit

Okazało się, że rozwiązanie w internecie jednak nie działa:

- (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

spróbuj tego: 'NSString * js = @" var meta = document.createElement ('meta'); " " meta.setAttribute ("nazwa", "widok"); " " meta.setAttribute ("zawartość", "szerokość = szerokość urządzenia"); " "document.getElementsByTagName ('głowa') [0] .appendChild (meta)"; [Webview stringByEvaluatingJavaScriptFromString: js];.' zastąpić kod z powyższego kodu – z22

+0

I spróbowaliśmy, ale mam takie same Wynik (przy okazji, metoda stringByEvaluatingJavaScriptFromString nie istnieje w WKWebView, więc zachowałem mój answerJavaScript: completeHandler wywołanie – olinsha

+0

dostałeś rozwiązanie? – anoop4real

Odpowiedz

81

można również spróbować WKUserScript.

oto mój config pracy:

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

można dodać dodatkowej konfiguracji do WKWebViewConfiguration.

+2

Działa jak urok.Dziękuję –

+1

Działa doskonale dla mnie - tylko drobne zmiany zostały zmienione WKUserScriptInjectionTimeAtDocumentEnd na WKUserScriptInjectionTimeAtDocumentStart – sckor

+0

To powinna być zaakceptowana odpowiedź. – Brams

7

podobne do @ nferocious76, ale w języku 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) 
+0

Dzięki! to również rozwiązało mój problem, aby rozmiar czcionki html stał się większy, nie odświeżał się od małego do dużego. Używam tego, aby "odświeżyć" rozmiar czcionki i wewnętrzny układ strony. –

16

Echo odpowiedzi nferocious76'S, w kodzie SWIFT: wersja 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 wersji

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# wersja do użytku w 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);