xcode 在 WKWebView 中禁用缩放?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40452034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable zoom in WKWebView?
提问by Bazley
Does anyone know a nice simple way to disable double-click and pinch zooming in a WKWebView? Nothing I've tried works:
有谁知道一种很好的简单方法来禁用 WKWebView 中的双击和双指缩放?我尝试过的一切都不起作用:
Webview.scrollView.allowsMagnification = false; // Error: value of type WKWebView has no member allowsMagnification
Webview.scrollView.isMultipleTouchEnabled = false; // doesn't do anything
In the html:
在 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
回答by pritam001
You will have to add maximum scale in script.
您必须在脚本中添加最大比例。
The following code should help you:
以下代码应该可以帮助您:
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)
回答by Ahmed Lotfy
Swift 4:
斯威夫特 4:
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
self.webView.scrollView.delegate = self
}
//MARK: - UIScrollViewDelegate
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
scrollView.pinchGestureRecognizer?.isEnabled = false
}
回答by Droid Chris
OK this is how I solved this:
好的,这就是我解决这个问题的方法:
wkWebView.configuration.userContentController.addUserScript(self.getZoomDisableScript())
private func getZoomDisableScript() -> WKUserScript {
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);"
return WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
}
回答by 93sauu
This post explain how managing the zoom.
这篇文章解释了如何管理缩放。
WebKit: New Interaction Behaviors in iOS 10
In iOS 10 "ignoresViewportScaleLimits" property was added to WKWebViewConfiguration, which is false by default. The post recommends to set to true and to manage correctly the zoom in the page.
在 iOS 10 中,“ignoresViewportScaleLimits”属性被添加到 WKWebViewConfiguration 中,默认情况下为 false。该帖子建议设置为 true 并正确管理页面的缩放。
Note: In Safari and SafariViewController the value for this property is true by default.
注意:在 Safari 和 SafariViewController 中,此属性的值默认为 true。
In my case I want to reset the zoom in some cases. For this the unique way that I found it was change maximum scale to 1.0 and after to 10.0.
在我的情况下,我想在某些情况下重置缩放。为此,我发现它的独特方式是将最大比例更改为 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];
}];
This code should works but I recommend to create the "viewport" always in the html and to create a function (i.e. resetZoom or disableZoom) in the javascript to call it directly from objective-C (it is cleaner)
这段代码应该可以工作,但我建议始终在 html 中创建“视口”,并在 javascript 中创建一个函数(即 resetZoom 或 disableZoom)以直接从 Objective-C 调用它(它更干净)
I hope it helps.
我希望它有帮助。
回答by sai Ma
it's works on iOS 13
它适用于 iOS 13
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);";
// you should use WKUserScript, `evaluateJavaScript` is not working
WKUserScript *us = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];[wkWebView.configuration.userContentController addUserScript:us];
not working
不工作
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return nil;
}
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
scrollView.pinchGestureRecognizer.enabled = NO;
}