【问题标题】:Scrolling with WKWebview with Swift 4使用 Swift 4 滚动 WKWebview
【发布时间】:2018-10-07 18:21:04
【问题描述】:

我有一个WKWebview 正确显示来自本地网络文件的 html 内容。 html 比屏幕宽得多,我需要启用水平滚动。仅使用WKWebview 是否可行,还是我需要将WKWebview 设为UIScrollView 的子视图?

【问题讨论】:

    标签: html ios uiscrollview swift4 wkwebview


    【解决方案1】:

    添加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)
    

    【讨论】:

    • 不幸的是,我不太了解 Objective-C。你能用 Swift 4 回答吗?
    猜你喜欢
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 2018-07-02
    相关资源
    最近更新 更多