有没有人知道在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
答案 0 :(得分:33)
您必须在脚本中添加最大比例。
以下代码可以帮助您:
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)
答案 1 :(得分:8)
@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
}
答案 2 :(得分:2)
这篇文章解释了如何管理缩放。
WebKit: New Interaction Behaviors in iOS 10
在iOS 10 "ignoresViewportScaleLimits" property was added to WKWebViewConfiguration中,默认为false。帖子建议设置为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];
}];
此代码应该有效,但我建议创建&#34; viewport&#34;总是在html中并在javascript中创建一个函数(即resetZoom或disableZoom)直接从objective-C调用它(它更干净)
我希望它有所帮助。
答案 3 :(得分:2)
它可以在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];
不起作用
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return nil;
}
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
scrollView.pinchGestureRecognizer.enabled = NO;
}
答案 4 :(得分:1)
好的,这就是我的解决方法:
MAP_PRIVATE