2016-11-06 285 views
0

有誰知道一個很好的簡單方法來禁用雙擊並捏縮放WKWebView?沒什麼我已經試過作品:禁用放大WKWebView?

Webview.scrollView.allowsMagnification = false; // Error: value of type WKWebView has no member allowsMagnification 

Webview.scrollView.isMultipleTouchEnabled = false; // doesn't do anything 

在HTML:

<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" /> // pile of crap, does nothing 

回答

10

你將不得不在script.Following添加最大規模的代碼應該幫助你

let source: String = "var meta = document.createElement('meta');" + 
      "meta.name = 'viewport';" + 
      "meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';" + 
      "var head = document.getElementsByTagName('head')[0];" + "head.appendChild(meta);"; 
      let script: WKUserScript = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true) 
      let userContentController: WKUserContentController = WKUserContentController() 
      let conf = WKWebViewConfiguration() 
      conf.userContentController = userContentController 
      userContentController.addUserScript(script) 
      let webView = WKWebView(frame: CGRect.zero, configuration: conf) 
+0

是的,這工作正常。謝謝。 –

0

這篇文章解釋如何管理縮放。

WebKit: New Interaction Behaviors in iOS 10

在iOS系統10 「ignoresViewportScaleLimits」 在WKWebView,這是默認假加入屬性。該帖子建議設置爲true並正確管理頁面中的縮放。

注意:在Safari和SafariViewController中,此屬性的值默認爲true。

在我的情況下,我想在某些情況下重置縮放。爲此,我發現它的唯一方式是將最大比例更改爲1.0,之後更改爲10.0。

NSString* js = 
    @"var meta = document.createElement('meta'); " \ 
    "meta.setAttribute('name', 'viewport'); " \ 
    "meta.setAttribute('content', 'width = device-width, initial-scale = 1.0, minimum-scale = 1.0, maximum-scale = 1.0, user-scalable = yes'); " \ 
    "document.getElementsByTagName('head')[0].appendChild(meta)"; 
    [_wkWebview evaluateJavaScript:js completionHandler:^(id _Nullable object, NSError * _Nullable error) { 
     NSString* js = 
     @"var meta = document.createElement('meta'); " \ 
     "meta.setAttribute('name', 'viewport'); " \ 
     "meta.setAttribute('content', 'width = device-width, initial-scale = 1.0, minimum-scale = 1.0, maximum-scale = 10.0, user-scalable = yes'); " \ 
     "document.getElementsByTagName('head')[0].appendChild(meta)"; 
     [_wkWebview evaluateJavaScript:js completionHandler:nil]; 
    }]; 

此代碼應工作,但我建議打造「視口」總是在HTML和JavaScript的創建功能(即resetZoom或disableZoom)直接從客觀-C調用它(它是清潔)

我希望它有幫助。