簡體   English   中英

使用 Swift 4 使用 WKWebview 滾動

[英]Scrolling with WKWebview with Swift 4

我有一個WKWebview正確顯示來自本地網絡文件的 html 內容。 html 比屏幕寬得多,我需要啟用水平滾動。 這是否可以僅使用WKWebview或者我是否需要將WKWebview UIScrollView的子視圖?

添加Javascript以根據設備修復屏幕寬度和高度。

使用此代碼:

NSString *js = @"var metaTag=document.createElement('meta');"
"metaTag.name = \"viewport\";"
"metaTag.content = \"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0\";"
"document.getElementsByTagName('head')[0].appendChild(metaTag);";

WKUserScript *script = [[WKUserScript alloc] initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
[_WebView.configuration.userContentController addUserScript:script];

在斯威夫特

let js = """
var metaTag=document.createElement('meta');\
metaTag.name = "viewport";\
metaTag.content = "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0";\
document.getElementsByTagName('head')[0].appendChild(metaTag);
"""

let script = WKUserScript(source: js, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
self.webView.configuration.userContentController.addUserScript(script)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM