Disable magnification gesture in WKWebView
![Multi tool use Multi tool use](http://sgv.ssvwv.com/sg/ssvwvcomimagb.png)
Multi tool use
I'm looking for a way to disable the "pinch to zoom" magnification gesture on the iOS implementation of WKWebView. There is a magnification BOOL property available for OS X but it doesn't seem to be available on iOS.
WKWebView.h
#if !TARGET_OS_IPHONE
/* @abstract A Boolean value indicating whether magnify gestures will
change the web view's magnification.
@discussion It is possible to set the magnification property even if
allowsMagnification is set to NO.
The default value is NO.
*/
@property (nonatomic) BOOL allowsMagnification;
I've, also, tried look at the WKWebView's gesture recognizers but that seems to be turning up an empty array. I'm assuming the actual recognizers are bured deeper in the component's structure (fairly complex, by the looks of it) and would rather not go digging for them if at all possible.
I know of possible hacks that could potentially disable the gesture from firing (selectively passing gestures to the WebView, add child view to capture pinch gesture, etc) but I've always found those introduce lag into the event and want to keep the implementation as clean/hack free as possible.
ios webkit ios8 wkwebview
add a comment |
I'm looking for a way to disable the "pinch to zoom" magnification gesture on the iOS implementation of WKWebView. There is a magnification BOOL property available for OS X but it doesn't seem to be available on iOS.
WKWebView.h
#if !TARGET_OS_IPHONE
/* @abstract A Boolean value indicating whether magnify gestures will
change the web view's magnification.
@discussion It is possible to set the magnification property even if
allowsMagnification is set to NO.
The default value is NO.
*/
@property (nonatomic) BOOL allowsMagnification;
I've, also, tried look at the WKWebView's gesture recognizers but that seems to be turning up an empty array. I'm assuming the actual recognizers are bured deeper in the component's structure (fairly complex, by the looks of it) and would rather not go digging for them if at all possible.
I know of possible hacks that could potentially disable the gesture from firing (selectively passing gestures to the WebView, add child view to capture pinch gesture, etc) but I've always found those introduce lag into the event and want to keep the implementation as clean/hack free as possible.
ios webkit ios8 wkwebview
add a comment |
I'm looking for a way to disable the "pinch to zoom" magnification gesture on the iOS implementation of WKWebView. There is a magnification BOOL property available for OS X but it doesn't seem to be available on iOS.
WKWebView.h
#if !TARGET_OS_IPHONE
/* @abstract A Boolean value indicating whether magnify gestures will
change the web view's magnification.
@discussion It is possible to set the magnification property even if
allowsMagnification is set to NO.
The default value is NO.
*/
@property (nonatomic) BOOL allowsMagnification;
I've, also, tried look at the WKWebView's gesture recognizers but that seems to be turning up an empty array. I'm assuming the actual recognizers are bured deeper in the component's structure (fairly complex, by the looks of it) and would rather not go digging for them if at all possible.
I know of possible hacks that could potentially disable the gesture from firing (selectively passing gestures to the WebView, add child view to capture pinch gesture, etc) but I've always found those introduce lag into the event and want to keep the implementation as clean/hack free as possible.
ios webkit ios8 wkwebview
I'm looking for a way to disable the "pinch to zoom" magnification gesture on the iOS implementation of WKWebView. There is a magnification BOOL property available for OS X but it doesn't seem to be available on iOS.
WKWebView.h
#if !TARGET_OS_IPHONE
/* @abstract A Boolean value indicating whether magnify gestures will
change the web view's magnification.
@discussion It is possible to set the magnification property even if
allowsMagnification is set to NO.
The default value is NO.
*/
@property (nonatomic) BOOL allowsMagnification;
I've, also, tried look at the WKWebView's gesture recognizers but that seems to be turning up an empty array. I'm assuming the actual recognizers are bured deeper in the component's structure (fairly complex, by the looks of it) and would rather not go digging for them if at all possible.
I know of possible hacks that could potentially disable the gesture from firing (selectively passing gestures to the WebView, add child view to capture pinch gesture, etc) but I've always found those introduce lag into the event and want to keep the implementation as clean/hack free as possible.
ios webkit ios8 wkwebview
ios webkit ios8 wkwebview
asked Aug 28 '14 at 16:31
KevinKevin
2,38811325
2,38811325
add a comment |
add a comment |
18 Answers
18
active
oldest
votes
You can prevent your users from zooming by setting the delegate of your WKWebKit's UIScrollView and implementing viewForZooming(in:)
as in the following:
class MyClass {
let webView = WKWebView()
init() {
super.init()
webView.scrollView.delegate = self
}
deinit() {
// Without this, it'll crash when your MyClass instance is deinit'd
webView.scrollView.delegate = nil
}
}
extension MyClass: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return nil
}
}
13
Don't forget to add conformance toUIScrollViewDelegate
, setwebView.scrollView.delegate = self;
and nullify the delegate pointer indeinit
.
– Marko Nikolovski
Oct 20 '15 at 14:17
2
This should be noted as the correct answer. A native iOS solution rather than trying to inject JS.
– C6Silver
Dec 3 '15 at 19:18
6
I disagree. While a native solution would be ideal, I think this solution is just as hacky as the JS one and is, potentially, more fragile. While WKWebView vends a UIScrollView, internally is uses a WKScrollView. While it is a subclass of UIScrollView, it ignores/overrides a lot of standard behavior (contentInset for one). Zooming an HTML document is very different then zooming a UIView, so I could see a future where this changes/no longer works. Since the JS solution uses W3C standards, it should always be supported.
– Kevin
Jan 28 '16 at 18:52
1
@Prcela Nullifying the delegate is not necessary. It's a weak variable and thus doesn't create retain cycles.
– Smongo
Aug 25 '17 at 17:08
8
Doesn't work for me.
– Oleksandr Cheboraka
May 24 '18 at 13:43
|
show 6 more comments
The below answer no longer works in iOS 10 beta.
To improve accessibility on websites in Safari, users can now
pinch-to-zoom even when a website sets user-scalable=no in the
viewport.
WKWebView seems to respect the viewport meta tag the same way Mobile Safari does (as to be expected). So, I found injecting that tag into the DOM through javascript after a page load does the trick. I would be weary of this solution unless you know exactly what HTML is being loaded into the webview, otherwise I suspect it would have unintended consequences. In my case, I'm loading HTML strings, so I can just add it to the HTML I ship with the app.
To do it generically for any webpage:
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
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);";
[webView evaluateJavaScript:javascript completionHandler:nil];
}
It might be wise to take a look at what kind of navigation has just been completed, since only a new page will need this javascript executed.
9
This still allows double tap zooming.minimum-scale=1.0
needs to be set to avoid this.
– fabb
Mar 1 '15 at 12:12
1
This answer helped, but I still seem to be able to create situations where it's possible to zoom. Not sure what or how yet.
– gravitron
Jun 3 '16 at 2:49
1
This is the correct answer — do not mess with the WKWebView's UIScrollView delegate.
– thefaj
Sep 29 '17 at 0:38
Thanks! Used this with Turbolinks and seems to work perfectly (on iOS 11). Can neither pinch nor double-tap to zoom.
– Henrik N
Oct 11 '17 at 12:09
Might be easier to do this with userScripts: stackoverflow.com/a/41741125/1683141
– Mdlc
Mar 30 '18 at 20:47
|
show 1 more comment
I have tried setting minimumZoomScale
and maximumZoomScale
properties of UIScrollView
to 1
or isMultipleTouchEnabled
property of UIView
to false
or returning nil
from invoking viewForZooming(in:)
of UIScrollViewDelegate
but none worked. In my case, after several trial and error, the following works in my case [Tested on iOS 10.3]:
class MyViewController: UIViewController {
var webView: WKWebView?
override viewDidLoad() {
super.viewDidLoad()
//...
self.webView.scrollView.delegate = self
//...
}
}
extension MyViewController: UIScrollViewDelegate {
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
scrollView.pinchGestureRecognizer?.isEnabled = false
}
}
1
Thanks! This is exactly what I want. The accepted answer cannot work for my case because after returnnil
in delegate, the scale of my webpage is wrong.
– Desmond DAI
Sep 11 '17 at 6:06
1
Perfect ! Use this , you can change the images frame through JS without any bad effect.
– guozqzzu
Oct 13 '17 at 8:09
2
Double tap-to-zoom will still work.
– Tamás Sengel
Aug 18 '18 at 22:12
1
I tried all options like you. Your answer was perfect for me. Thanks :)
– Ronaldo Albertini
Dec 12 '18 at 17:53
add a comment |
Full Swift 3 / iOS 10 version of Landschaft's answer:
import UIKit
import WebKit
class MyViewController: UIViewController, UIScrollViewDelegate {
var webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(webView)
webView.scrollView.delegate = self
}
// Disable zooming in webView
func viewForZooming(in: UIScrollView) -> UIView? {
return nil;
}
}
this one worked for me.
– Ashkan Ghodrat
Oct 29 '18 at 14:50
add a comment |
The native solutions were not working for me, and injecting JS is not ideal. I noticed that when a zoom occurs and my delegate is called, the pinchGestureRecognizer is enabled even though I disabled it when initializing the webview. To fix this, I set it to disabled whenever a zoom starts:
extension ViewController: UIScrollViewDelegate {
// disable zooming in webview
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
scrollView.pinchGestureRecognizer?.isEnabled = false
}
}
This works for me (for Swift 4)
– akr
Aug 21 '18 at 12:25
add a comment |
Complete working code to disable zooming in WkWebView in swift
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
// @IBOutlet var eventWkWebView: WKWebView!
var webView : WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration:webConfiguration)
webView.uiDelegate = self
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)
webView.configuration.userContentController.addUserScript(script)
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// view.addSubview(eventWkWebView)
let myUrl = URL(string: "https://www.google.com")
let myRequest = URLRequest(url: myUrl!)
webView.load(myRequest)
}
}
1
the only solution that worked for me iOS 11.4. I have no idea why the other solutions, especially native ones did not work but javascript trick did... Crazy how easy is to develop on Android and how difficult on iOS. Programming should be simple, no?
– undefinedman
Jun 22 '18 at 23:53
add a comment |
In case you display a local html you could just modify this html and put this meta tag to your html:
<head>
<meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='viewport' />
</head>
add a comment |
Swift 2.0
extension WKWebView {
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return nil
}
}
Note that would do it to all WKWebViews
– Luke
Mar 15 '17 at 5:35
1
Not if you subclass it first and apply the extension to the subclass. Works for me.
– BaseZen
Mar 31 '17 at 21:38
1
This doesn't work (anymore?) in iOS 12.
– alexkent
Sep 26 '18 at 12:06
add a comment |
You can use UIScrollViewDelegate for this. First assign delegate to your webview in viewDidLoad() or any other suitable method as:
class LoginViewController: UIViewController, WKUIDelegate, UIScrollViewDelegate {
override func viewDidLoad() {
webView.scrollView.delegate = self
}
//And add this delegate method in your controller:
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
scrollView.pinchGestureRecognizer?.isEnabled = false
scrollView.panGestureRecognizer.isEnabled = false
}
}
This one worked for me on iOS 12. Other solutions like viewport or return nil for viewForZooming didn't
– Nemanja
Nov 3 '18 at 6:10
add a comment |
this is how I disabled zoom for Swift3 view controller for one-webview-only app
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate, UIScrollViewDelegate {
@IBOutlet var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
webView.scrollView.delegate = self
}
func viewForZooming(in: UIScrollView) -> UIView? {
return nil;
}
}
Finally! Man it's tough tracking down the constant Swift changes (nearly everything else on the web has the delegate func being "viewForZoomingInScrollView". Thanks for this update to "viewForZooming"
– Matt Gardner
Sep 15 '17 at 20:53
@thefaj this solution works fine for me, thats it
– godblessstrawberry
Sep 29 '17 at 8:15
add a comment |
I don't have enough reputation to add comments to answers, but I wanted to mention that Kevin's solution (meta viewport) no longer works in iOS 10 beta. Landschaft's solution is working for me, though!
Since the JS solution uses W3C standards, it should always be supported.
Ah, Kevin, I wish that were how these things worked.
More here: https://stackoverflow.com/a/37859168/1389714
1
Welp. So it goes, I suppose. Changing the correct answer, thanks for commenting.
– Kevin
Jul 13 '16 at 15:53
add a comment |
If it's not important for you to handle links inside html (say you want to display text only) the simplest would be to turn off user interaction
webView.userInteractionEnabled = false
This will disable url tapping
– Timur Bernikovich
Jan 30 '17 at 12:39
Hence I wrote "If it's not important for you to handle links inside html "
– lvp
Jan 30 '17 at 12:41
This will disable scrolling! The OP wants to the disable pinch to zoom but I guess he wants a usable webview.
– tanzolone
Mar 6 '17 at 18:02
add a comment |
Disable double tap to zoom gesture by require failure of gesture recognizer. It will prevent the browser to take action when user double tap.
import UIKit
class DisableDoubleTapRecognizer : UITapGestureRecognizer, UIGestureRecognizerDelegate{
override init(target: Any?, action: Selector?) {
super.init(target: target, action: action)
}
init() {
super.init(target:nil, action: nil)
self.numberOfTapsRequired = 2;
self.delegate = self;
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true;
}
}
//in your view controller
override func viewDidLoad() {
super.viewDidLoad()
webView.addGestureRecognizer(DisableDoubleTapRecognizer())
}
add a comment |
We need to change the delegate call with following signatures in XCode 8:
override func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return nil
}
add a comment |
If the webview is user for read-only purposes, i.e, just for displaying the web content use
webView.isUserInteractionEnabled = false
By this the zoom gesture won't work on it.
This will also disable scrolling. Just because the content is read-only does not mean you cannot scroll through it.
– Bassem Sameh
May 18 '18 at 11:04
add a comment |
Here's a slightly modified version of Gulshan Kumar's answer that worked for me in iOS 12.1.2, and it also prevents zooming due to double-taps and rotation. In my example, I just inject the script directly into the web view. I adjusted the scale to 60%, and there's no need to use concatenation in building up the source string.
let source = "var meta = document.createElement('meta'); meta.name = 'viewport'; meta.content = 'width=device-width, initial-scale=0.6, maximum-scale=0.6, user-scalable=no'; var head = document.getElementsByTagName('head')[0]; head.appendChild(meta);"
let script = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
webView.configuration.userContentController.addUserScript(script)
add a comment |
The application I work on needed the view to be zoomed by Javascript. The accepted answer blocked zooming by JavaScript from inside the page too.
I only needed to disable pinch gesture by the user of the appliction. The only solution I've found is to disable the gesture of the web view after the page has loaded:
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
/* disable pinch gesture recognizer to allow zooming the web view by code but prevent zooming it by the user */
for (UIGestureRecognizer *gr in self.webView.scrollView.gestureRecognizers) {
if ([gr isKindOfClass:[UIPinchGestureRecognizer class]]) {
gr.enabled = NO;
}
}
}
add a comment |
This worked for me. https://gist.github.com/paulofierro/5b642dcde5ee9e86a130
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)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25553711%2fdisable-magnification-gesture-in-wkwebview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
18 Answers
18
active
oldest
votes
18 Answers
18
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can prevent your users from zooming by setting the delegate of your WKWebKit's UIScrollView and implementing viewForZooming(in:)
as in the following:
class MyClass {
let webView = WKWebView()
init() {
super.init()
webView.scrollView.delegate = self
}
deinit() {
// Without this, it'll crash when your MyClass instance is deinit'd
webView.scrollView.delegate = nil
}
}
extension MyClass: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return nil
}
}
13
Don't forget to add conformance toUIScrollViewDelegate
, setwebView.scrollView.delegate = self;
and nullify the delegate pointer indeinit
.
– Marko Nikolovski
Oct 20 '15 at 14:17
2
This should be noted as the correct answer. A native iOS solution rather than trying to inject JS.
– C6Silver
Dec 3 '15 at 19:18
6
I disagree. While a native solution would be ideal, I think this solution is just as hacky as the JS one and is, potentially, more fragile. While WKWebView vends a UIScrollView, internally is uses a WKScrollView. While it is a subclass of UIScrollView, it ignores/overrides a lot of standard behavior (contentInset for one). Zooming an HTML document is very different then zooming a UIView, so I could see a future where this changes/no longer works. Since the JS solution uses W3C standards, it should always be supported.
– Kevin
Jan 28 '16 at 18:52
1
@Prcela Nullifying the delegate is not necessary. It's a weak variable and thus doesn't create retain cycles.
– Smongo
Aug 25 '17 at 17:08
8
Doesn't work for me.
– Oleksandr Cheboraka
May 24 '18 at 13:43
|
show 6 more comments
You can prevent your users from zooming by setting the delegate of your WKWebKit's UIScrollView and implementing viewForZooming(in:)
as in the following:
class MyClass {
let webView = WKWebView()
init() {
super.init()
webView.scrollView.delegate = self
}
deinit() {
// Without this, it'll crash when your MyClass instance is deinit'd
webView.scrollView.delegate = nil
}
}
extension MyClass: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return nil
}
}
13
Don't forget to add conformance toUIScrollViewDelegate
, setwebView.scrollView.delegate = self;
and nullify the delegate pointer indeinit
.
– Marko Nikolovski
Oct 20 '15 at 14:17
2
This should be noted as the correct answer. A native iOS solution rather than trying to inject JS.
– C6Silver
Dec 3 '15 at 19:18
6
I disagree. While a native solution would be ideal, I think this solution is just as hacky as the JS one and is, potentially, more fragile. While WKWebView vends a UIScrollView, internally is uses a WKScrollView. While it is a subclass of UIScrollView, it ignores/overrides a lot of standard behavior (contentInset for one). Zooming an HTML document is very different then zooming a UIView, so I could see a future where this changes/no longer works. Since the JS solution uses W3C standards, it should always be supported.
– Kevin
Jan 28 '16 at 18:52
1
@Prcela Nullifying the delegate is not necessary. It's a weak variable and thus doesn't create retain cycles.
– Smongo
Aug 25 '17 at 17:08
8
Doesn't work for me.
– Oleksandr Cheboraka
May 24 '18 at 13:43
|
show 6 more comments
You can prevent your users from zooming by setting the delegate of your WKWebKit's UIScrollView and implementing viewForZooming(in:)
as in the following:
class MyClass {
let webView = WKWebView()
init() {
super.init()
webView.scrollView.delegate = self
}
deinit() {
// Without this, it'll crash when your MyClass instance is deinit'd
webView.scrollView.delegate = nil
}
}
extension MyClass: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return nil
}
}
You can prevent your users from zooming by setting the delegate of your WKWebKit's UIScrollView and implementing viewForZooming(in:)
as in the following:
class MyClass {
let webView = WKWebView()
init() {
super.init()
webView.scrollView.delegate = self
}
deinit() {
// Without this, it'll crash when your MyClass instance is deinit'd
webView.scrollView.delegate = nil
}
}
extension MyClass: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return nil
}
}
edited Jan 24 '18 at 2:32
![](https://i.stack.imgur.com/esf9c.png?s=32&g=1)
![](https://i.stack.imgur.com/esf9c.png?s=32&g=1)
Sandy Chapman
8,29934352
8,29934352
answered Aug 11 '15 at 14:02
![](https://i.stack.imgur.com/vmdku.png?s=32&g=1)
![](https://i.stack.imgur.com/vmdku.png?s=32&g=1)
LandschaftLandschaft
81089
81089
13
Don't forget to add conformance toUIScrollViewDelegate
, setwebView.scrollView.delegate = self;
and nullify the delegate pointer indeinit
.
– Marko Nikolovski
Oct 20 '15 at 14:17
2
This should be noted as the correct answer. A native iOS solution rather than trying to inject JS.
– C6Silver
Dec 3 '15 at 19:18
6
I disagree. While a native solution would be ideal, I think this solution is just as hacky as the JS one and is, potentially, more fragile. While WKWebView vends a UIScrollView, internally is uses a WKScrollView. While it is a subclass of UIScrollView, it ignores/overrides a lot of standard behavior (contentInset for one). Zooming an HTML document is very different then zooming a UIView, so I could see a future where this changes/no longer works. Since the JS solution uses W3C standards, it should always be supported.
– Kevin
Jan 28 '16 at 18:52
1
@Prcela Nullifying the delegate is not necessary. It's a weak variable and thus doesn't create retain cycles.
– Smongo
Aug 25 '17 at 17:08
8
Doesn't work for me.
– Oleksandr Cheboraka
May 24 '18 at 13:43
|
show 6 more comments
13
Don't forget to add conformance toUIScrollViewDelegate
, setwebView.scrollView.delegate = self;
and nullify the delegate pointer indeinit
.
– Marko Nikolovski
Oct 20 '15 at 14:17
2
This should be noted as the correct answer. A native iOS solution rather than trying to inject JS.
– C6Silver
Dec 3 '15 at 19:18
6
I disagree. While a native solution would be ideal, I think this solution is just as hacky as the JS one and is, potentially, more fragile. While WKWebView vends a UIScrollView, internally is uses a WKScrollView. While it is a subclass of UIScrollView, it ignores/overrides a lot of standard behavior (contentInset for one). Zooming an HTML document is very different then zooming a UIView, so I could see a future where this changes/no longer works. Since the JS solution uses W3C standards, it should always be supported.
– Kevin
Jan 28 '16 at 18:52
1
@Prcela Nullifying the delegate is not necessary. It's a weak variable and thus doesn't create retain cycles.
– Smongo
Aug 25 '17 at 17:08
8
Doesn't work for me.
– Oleksandr Cheboraka
May 24 '18 at 13:43
13
13
Don't forget to add conformance to
UIScrollViewDelegate
, set webView.scrollView.delegate = self;
and nullify the delegate pointer in deinit
.– Marko Nikolovski
Oct 20 '15 at 14:17
Don't forget to add conformance to
UIScrollViewDelegate
, set webView.scrollView.delegate = self;
and nullify the delegate pointer in deinit
.– Marko Nikolovski
Oct 20 '15 at 14:17
2
2
This should be noted as the correct answer. A native iOS solution rather than trying to inject JS.
– C6Silver
Dec 3 '15 at 19:18
This should be noted as the correct answer. A native iOS solution rather than trying to inject JS.
– C6Silver
Dec 3 '15 at 19:18
6
6
I disagree. While a native solution would be ideal, I think this solution is just as hacky as the JS one and is, potentially, more fragile. While WKWebView vends a UIScrollView, internally is uses a WKScrollView. While it is a subclass of UIScrollView, it ignores/overrides a lot of standard behavior (contentInset for one). Zooming an HTML document is very different then zooming a UIView, so I could see a future where this changes/no longer works. Since the JS solution uses W3C standards, it should always be supported.
– Kevin
Jan 28 '16 at 18:52
I disagree. While a native solution would be ideal, I think this solution is just as hacky as the JS one and is, potentially, more fragile. While WKWebView vends a UIScrollView, internally is uses a WKScrollView. While it is a subclass of UIScrollView, it ignores/overrides a lot of standard behavior (contentInset for one). Zooming an HTML document is very different then zooming a UIView, so I could see a future where this changes/no longer works. Since the JS solution uses W3C standards, it should always be supported.
– Kevin
Jan 28 '16 at 18:52
1
1
@Prcela Nullifying the delegate is not necessary. It's a weak variable and thus doesn't create retain cycles.
– Smongo
Aug 25 '17 at 17:08
@Prcela Nullifying the delegate is not necessary. It's a weak variable and thus doesn't create retain cycles.
– Smongo
Aug 25 '17 at 17:08
8
8
Doesn't work for me.
– Oleksandr Cheboraka
May 24 '18 at 13:43
Doesn't work for me.
– Oleksandr Cheboraka
May 24 '18 at 13:43
|
show 6 more comments
The below answer no longer works in iOS 10 beta.
To improve accessibility on websites in Safari, users can now
pinch-to-zoom even when a website sets user-scalable=no in the
viewport.
WKWebView seems to respect the viewport meta tag the same way Mobile Safari does (as to be expected). So, I found injecting that tag into the DOM through javascript after a page load does the trick. I would be weary of this solution unless you know exactly what HTML is being loaded into the webview, otherwise I suspect it would have unintended consequences. In my case, I'm loading HTML strings, so I can just add it to the HTML I ship with the app.
To do it generically for any webpage:
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
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);";
[webView evaluateJavaScript:javascript completionHandler:nil];
}
It might be wise to take a look at what kind of navigation has just been completed, since only a new page will need this javascript executed.
9
This still allows double tap zooming.minimum-scale=1.0
needs to be set to avoid this.
– fabb
Mar 1 '15 at 12:12
1
This answer helped, but I still seem to be able to create situations where it's possible to zoom. Not sure what or how yet.
– gravitron
Jun 3 '16 at 2:49
1
This is the correct answer — do not mess with the WKWebView's UIScrollView delegate.
– thefaj
Sep 29 '17 at 0:38
Thanks! Used this with Turbolinks and seems to work perfectly (on iOS 11). Can neither pinch nor double-tap to zoom.
– Henrik N
Oct 11 '17 at 12:09
Might be easier to do this with userScripts: stackoverflow.com/a/41741125/1683141
– Mdlc
Mar 30 '18 at 20:47
|
show 1 more comment
The below answer no longer works in iOS 10 beta.
To improve accessibility on websites in Safari, users can now
pinch-to-zoom even when a website sets user-scalable=no in the
viewport.
WKWebView seems to respect the viewport meta tag the same way Mobile Safari does (as to be expected). So, I found injecting that tag into the DOM through javascript after a page load does the trick. I would be weary of this solution unless you know exactly what HTML is being loaded into the webview, otherwise I suspect it would have unintended consequences. In my case, I'm loading HTML strings, so I can just add it to the HTML I ship with the app.
To do it generically for any webpage:
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
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);";
[webView evaluateJavaScript:javascript completionHandler:nil];
}
It might be wise to take a look at what kind of navigation has just been completed, since only a new page will need this javascript executed.
9
This still allows double tap zooming.minimum-scale=1.0
needs to be set to avoid this.
– fabb
Mar 1 '15 at 12:12
1
This answer helped, but I still seem to be able to create situations where it's possible to zoom. Not sure what or how yet.
– gravitron
Jun 3 '16 at 2:49
1
This is the correct answer — do not mess with the WKWebView's UIScrollView delegate.
– thefaj
Sep 29 '17 at 0:38
Thanks! Used this with Turbolinks and seems to work perfectly (on iOS 11). Can neither pinch nor double-tap to zoom.
– Henrik N
Oct 11 '17 at 12:09
Might be easier to do this with userScripts: stackoverflow.com/a/41741125/1683141
– Mdlc
Mar 30 '18 at 20:47
|
show 1 more comment
The below answer no longer works in iOS 10 beta.
To improve accessibility on websites in Safari, users can now
pinch-to-zoom even when a website sets user-scalable=no in the
viewport.
WKWebView seems to respect the viewport meta tag the same way Mobile Safari does (as to be expected). So, I found injecting that tag into the DOM through javascript after a page load does the trick. I would be weary of this solution unless you know exactly what HTML is being loaded into the webview, otherwise I suspect it would have unintended consequences. In my case, I'm loading HTML strings, so I can just add it to the HTML I ship with the app.
To do it generically for any webpage:
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
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);";
[webView evaluateJavaScript:javascript completionHandler:nil];
}
It might be wise to take a look at what kind of navigation has just been completed, since only a new page will need this javascript executed.
The below answer no longer works in iOS 10 beta.
To improve accessibility on websites in Safari, users can now
pinch-to-zoom even when a website sets user-scalable=no in the
viewport.
WKWebView seems to respect the viewport meta tag the same way Mobile Safari does (as to be expected). So, I found injecting that tag into the DOM through javascript after a page load does the trick. I would be weary of this solution unless you know exactly what HTML is being loaded into the webview, otherwise I suspect it would have unintended consequences. In my case, I'm loading HTML strings, so I can just add it to the HTML I ship with the app.
To do it generically for any webpage:
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
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);";
[webView evaluateJavaScript:javascript completionHandler:nil];
}
It might be wise to take a look at what kind of navigation has just been completed, since only a new page will need this javascript executed.
edited Jul 13 '16 at 15:55
answered Sep 3 '14 at 15:05
KevinKevin
2,38811325
2,38811325
9
This still allows double tap zooming.minimum-scale=1.0
needs to be set to avoid this.
– fabb
Mar 1 '15 at 12:12
1
This answer helped, but I still seem to be able to create situations where it's possible to zoom. Not sure what or how yet.
– gravitron
Jun 3 '16 at 2:49
1
This is the correct answer — do not mess with the WKWebView's UIScrollView delegate.
– thefaj
Sep 29 '17 at 0:38
Thanks! Used this with Turbolinks and seems to work perfectly (on iOS 11). Can neither pinch nor double-tap to zoom.
– Henrik N
Oct 11 '17 at 12:09
Might be easier to do this with userScripts: stackoverflow.com/a/41741125/1683141
– Mdlc
Mar 30 '18 at 20:47
|
show 1 more comment
9
This still allows double tap zooming.minimum-scale=1.0
needs to be set to avoid this.
– fabb
Mar 1 '15 at 12:12
1
This answer helped, but I still seem to be able to create situations where it's possible to zoom. Not sure what or how yet.
– gravitron
Jun 3 '16 at 2:49
1
This is the correct answer — do not mess with the WKWebView's UIScrollView delegate.
– thefaj
Sep 29 '17 at 0:38
Thanks! Used this with Turbolinks and seems to work perfectly (on iOS 11). Can neither pinch nor double-tap to zoom.
– Henrik N
Oct 11 '17 at 12:09
Might be easier to do this with userScripts: stackoverflow.com/a/41741125/1683141
– Mdlc
Mar 30 '18 at 20:47
9
9
This still allows double tap zooming.
minimum-scale=1.0
needs to be set to avoid this.– fabb
Mar 1 '15 at 12:12
This still allows double tap zooming.
minimum-scale=1.0
needs to be set to avoid this.– fabb
Mar 1 '15 at 12:12
1
1
This answer helped, but I still seem to be able to create situations where it's possible to zoom. Not sure what or how yet.
– gravitron
Jun 3 '16 at 2:49
This answer helped, but I still seem to be able to create situations where it's possible to zoom. Not sure what or how yet.
– gravitron
Jun 3 '16 at 2:49
1
1
This is the correct answer — do not mess with the WKWebView's UIScrollView delegate.
– thefaj
Sep 29 '17 at 0:38
This is the correct answer — do not mess with the WKWebView's UIScrollView delegate.
– thefaj
Sep 29 '17 at 0:38
Thanks! Used this with Turbolinks and seems to work perfectly (on iOS 11). Can neither pinch nor double-tap to zoom.
– Henrik N
Oct 11 '17 at 12:09
Thanks! Used this with Turbolinks and seems to work perfectly (on iOS 11). Can neither pinch nor double-tap to zoom.
– Henrik N
Oct 11 '17 at 12:09
Might be easier to do this with userScripts: stackoverflow.com/a/41741125/1683141
– Mdlc
Mar 30 '18 at 20:47
Might be easier to do this with userScripts: stackoverflow.com/a/41741125/1683141
– Mdlc
Mar 30 '18 at 20:47
|
show 1 more comment
I have tried setting minimumZoomScale
and maximumZoomScale
properties of UIScrollView
to 1
or isMultipleTouchEnabled
property of UIView
to false
or returning nil
from invoking viewForZooming(in:)
of UIScrollViewDelegate
but none worked. In my case, after several trial and error, the following works in my case [Tested on iOS 10.3]:
class MyViewController: UIViewController {
var webView: WKWebView?
override viewDidLoad() {
super.viewDidLoad()
//...
self.webView.scrollView.delegate = self
//...
}
}
extension MyViewController: UIScrollViewDelegate {
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
scrollView.pinchGestureRecognizer?.isEnabled = false
}
}
1
Thanks! This is exactly what I want. The accepted answer cannot work for my case because after returnnil
in delegate, the scale of my webpage is wrong.
– Desmond DAI
Sep 11 '17 at 6:06
1
Perfect ! Use this , you can change the images frame through JS without any bad effect.
– guozqzzu
Oct 13 '17 at 8:09
2
Double tap-to-zoom will still work.
– Tamás Sengel
Aug 18 '18 at 22:12
1
I tried all options like you. Your answer was perfect for me. Thanks :)
– Ronaldo Albertini
Dec 12 '18 at 17:53
add a comment |
I have tried setting minimumZoomScale
and maximumZoomScale
properties of UIScrollView
to 1
or isMultipleTouchEnabled
property of UIView
to false
or returning nil
from invoking viewForZooming(in:)
of UIScrollViewDelegate
but none worked. In my case, after several trial and error, the following works in my case [Tested on iOS 10.3]:
class MyViewController: UIViewController {
var webView: WKWebView?
override viewDidLoad() {
super.viewDidLoad()
//...
self.webView.scrollView.delegate = self
//...
}
}
extension MyViewController: UIScrollViewDelegate {
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
scrollView.pinchGestureRecognizer?.isEnabled = false
}
}
1
Thanks! This is exactly what I want. The accepted answer cannot work for my case because after returnnil
in delegate, the scale of my webpage is wrong.
– Desmond DAI
Sep 11 '17 at 6:06
1
Perfect ! Use this , you can change the images frame through JS without any bad effect.
– guozqzzu
Oct 13 '17 at 8:09
2
Double tap-to-zoom will still work.
– Tamás Sengel
Aug 18 '18 at 22:12
1
I tried all options like you. Your answer was perfect for me. Thanks :)
– Ronaldo Albertini
Dec 12 '18 at 17:53
add a comment |
I have tried setting minimumZoomScale
and maximumZoomScale
properties of UIScrollView
to 1
or isMultipleTouchEnabled
property of UIView
to false
or returning nil
from invoking viewForZooming(in:)
of UIScrollViewDelegate
but none worked. In my case, after several trial and error, the following works in my case [Tested on iOS 10.3]:
class MyViewController: UIViewController {
var webView: WKWebView?
override viewDidLoad() {
super.viewDidLoad()
//...
self.webView.scrollView.delegate = self
//...
}
}
extension MyViewController: UIScrollViewDelegate {
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
scrollView.pinchGestureRecognizer?.isEnabled = false
}
}
I have tried setting minimumZoomScale
and maximumZoomScale
properties of UIScrollView
to 1
or isMultipleTouchEnabled
property of UIView
to false
or returning nil
from invoking viewForZooming(in:)
of UIScrollViewDelegate
but none worked. In my case, after several trial and error, the following works in my case [Tested on iOS 10.3]:
class MyViewController: UIViewController {
var webView: WKWebView?
override viewDidLoad() {
super.viewDidLoad()
//...
self.webView.scrollView.delegate = self
//...
}
}
extension MyViewController: UIScrollViewDelegate {
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
scrollView.pinchGestureRecognizer?.isEnabled = false
}
}
answered Apr 4 '17 at 11:04
![](https://i.stack.imgur.com/NmGuu.jpg?s=32&g=1)
![](https://i.stack.imgur.com/NmGuu.jpg?s=32&g=1)
yohannesyohannes
577412
577412
1
Thanks! This is exactly what I want. The accepted answer cannot work for my case because after returnnil
in delegate, the scale of my webpage is wrong.
– Desmond DAI
Sep 11 '17 at 6:06
1
Perfect ! Use this , you can change the images frame through JS without any bad effect.
– guozqzzu
Oct 13 '17 at 8:09
2
Double tap-to-zoom will still work.
– Tamás Sengel
Aug 18 '18 at 22:12
1
I tried all options like you. Your answer was perfect for me. Thanks :)
– Ronaldo Albertini
Dec 12 '18 at 17:53
add a comment |
1
Thanks! This is exactly what I want. The accepted answer cannot work for my case because after returnnil
in delegate, the scale of my webpage is wrong.
– Desmond DAI
Sep 11 '17 at 6:06
1
Perfect ! Use this , you can change the images frame through JS without any bad effect.
– guozqzzu
Oct 13 '17 at 8:09
2
Double tap-to-zoom will still work.
– Tamás Sengel
Aug 18 '18 at 22:12
1
I tried all options like you. Your answer was perfect for me. Thanks :)
– Ronaldo Albertini
Dec 12 '18 at 17:53
1
1
Thanks! This is exactly what I want. The accepted answer cannot work for my case because after return
nil
in delegate, the scale of my webpage is wrong.– Desmond DAI
Sep 11 '17 at 6:06
Thanks! This is exactly what I want. The accepted answer cannot work for my case because after return
nil
in delegate, the scale of my webpage is wrong.– Desmond DAI
Sep 11 '17 at 6:06
1
1
Perfect ! Use this , you can change the images frame through JS without any bad effect.
– guozqzzu
Oct 13 '17 at 8:09
Perfect ! Use this , you can change the images frame through JS without any bad effect.
– guozqzzu
Oct 13 '17 at 8:09
2
2
Double tap-to-zoom will still work.
– Tamás Sengel
Aug 18 '18 at 22:12
Double tap-to-zoom will still work.
– Tamás Sengel
Aug 18 '18 at 22:12
1
1
I tried all options like you. Your answer was perfect for me. Thanks :)
– Ronaldo Albertini
Dec 12 '18 at 17:53
I tried all options like you. Your answer was perfect for me. Thanks :)
– Ronaldo Albertini
Dec 12 '18 at 17:53
add a comment |
Full Swift 3 / iOS 10 version of Landschaft's answer:
import UIKit
import WebKit
class MyViewController: UIViewController, UIScrollViewDelegate {
var webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(webView)
webView.scrollView.delegate = self
}
// Disable zooming in webView
func viewForZooming(in: UIScrollView) -> UIView? {
return nil;
}
}
this one worked for me.
– Ashkan Ghodrat
Oct 29 '18 at 14:50
add a comment |
Full Swift 3 / iOS 10 version of Landschaft's answer:
import UIKit
import WebKit
class MyViewController: UIViewController, UIScrollViewDelegate {
var webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(webView)
webView.scrollView.delegate = self
}
// Disable zooming in webView
func viewForZooming(in: UIScrollView) -> UIView? {
return nil;
}
}
this one worked for me.
– Ashkan Ghodrat
Oct 29 '18 at 14:50
add a comment |
Full Swift 3 / iOS 10 version of Landschaft's answer:
import UIKit
import WebKit
class MyViewController: UIViewController, UIScrollViewDelegate {
var webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(webView)
webView.scrollView.delegate = self
}
// Disable zooming in webView
func viewForZooming(in: UIScrollView) -> UIView? {
return nil;
}
}
Full Swift 3 / iOS 10 version of Landschaft's answer:
import UIKit
import WebKit
class MyViewController: UIViewController, UIScrollViewDelegate {
var webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(webView)
webView.scrollView.delegate = self
}
// Disable zooming in webView
func viewForZooming(in: UIScrollView) -> UIView? {
return nil;
}
}
edited May 4 '17 at 9:21
answered Mar 2 '17 at 16:25
Simon EpskampSimon Epskamp
4,56613340
4,56613340
this one worked for me.
– Ashkan Ghodrat
Oct 29 '18 at 14:50
add a comment |
this one worked for me.
– Ashkan Ghodrat
Oct 29 '18 at 14:50
this one worked for me.
– Ashkan Ghodrat
Oct 29 '18 at 14:50
this one worked for me.
– Ashkan Ghodrat
Oct 29 '18 at 14:50
add a comment |
The native solutions were not working for me, and injecting JS is not ideal. I noticed that when a zoom occurs and my delegate is called, the pinchGestureRecognizer is enabled even though I disabled it when initializing the webview. To fix this, I set it to disabled whenever a zoom starts:
extension ViewController: UIScrollViewDelegate {
// disable zooming in webview
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
scrollView.pinchGestureRecognizer?.isEnabled = false
}
}
This works for me (for Swift 4)
– akr
Aug 21 '18 at 12:25
add a comment |
The native solutions were not working for me, and injecting JS is not ideal. I noticed that when a zoom occurs and my delegate is called, the pinchGestureRecognizer is enabled even though I disabled it when initializing the webview. To fix this, I set it to disabled whenever a zoom starts:
extension ViewController: UIScrollViewDelegate {
// disable zooming in webview
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
scrollView.pinchGestureRecognizer?.isEnabled = false
}
}
This works for me (for Swift 4)
– akr
Aug 21 '18 at 12:25
add a comment |
The native solutions were not working for me, and injecting JS is not ideal. I noticed that when a zoom occurs and my delegate is called, the pinchGestureRecognizer is enabled even though I disabled it when initializing the webview. To fix this, I set it to disabled whenever a zoom starts:
extension ViewController: UIScrollViewDelegate {
// disable zooming in webview
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
scrollView.pinchGestureRecognizer?.isEnabled = false
}
}
The native solutions were not working for me, and injecting JS is not ideal. I noticed that when a zoom occurs and my delegate is called, the pinchGestureRecognizer is enabled even though I disabled it when initializing the webview. To fix this, I set it to disabled whenever a zoom starts:
extension ViewController: UIScrollViewDelegate {
// disable zooming in webview
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
scrollView.pinchGestureRecognizer?.isEnabled = false
}
}
edited Jul 27 '18 at 16:41
answered Jun 6 '18 at 21:24
pulse4lifepulse4life
783813
783813
This works for me (for Swift 4)
– akr
Aug 21 '18 at 12:25
add a comment |
This works for me (for Swift 4)
– akr
Aug 21 '18 at 12:25
This works for me (for Swift 4)
– akr
Aug 21 '18 at 12:25
This works for me (for Swift 4)
– akr
Aug 21 '18 at 12:25
add a comment |
Complete working code to disable zooming in WkWebView in swift
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
// @IBOutlet var eventWkWebView: WKWebView!
var webView : WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration:webConfiguration)
webView.uiDelegate = self
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)
webView.configuration.userContentController.addUserScript(script)
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// view.addSubview(eventWkWebView)
let myUrl = URL(string: "https://www.google.com")
let myRequest = URLRequest(url: myUrl!)
webView.load(myRequest)
}
}
1
the only solution that worked for me iOS 11.4. I have no idea why the other solutions, especially native ones did not work but javascript trick did... Crazy how easy is to develop on Android and how difficult on iOS. Programming should be simple, no?
– undefinedman
Jun 22 '18 at 23:53
add a comment |
Complete working code to disable zooming in WkWebView in swift
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
// @IBOutlet var eventWkWebView: WKWebView!
var webView : WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration:webConfiguration)
webView.uiDelegate = self
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)
webView.configuration.userContentController.addUserScript(script)
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// view.addSubview(eventWkWebView)
let myUrl = URL(string: "https://www.google.com")
let myRequest = URLRequest(url: myUrl!)
webView.load(myRequest)
}
}
1
the only solution that worked for me iOS 11.4. I have no idea why the other solutions, especially native ones did not work but javascript trick did... Crazy how easy is to develop on Android and how difficult on iOS. Programming should be simple, no?
– undefinedman
Jun 22 '18 at 23:53
add a comment |
Complete working code to disable zooming in WkWebView in swift
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
// @IBOutlet var eventWkWebView: WKWebView!
var webView : WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration:webConfiguration)
webView.uiDelegate = self
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)
webView.configuration.userContentController.addUserScript(script)
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// view.addSubview(eventWkWebView)
let myUrl = URL(string: "https://www.google.com")
let myRequest = URLRequest(url: myUrl!)
webView.load(myRequest)
}
}
Complete working code to disable zooming in WkWebView in swift
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
// @IBOutlet var eventWkWebView: WKWebView!
var webView : WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration:webConfiguration)
webView.uiDelegate = self
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)
webView.configuration.userContentController.addUserScript(script)
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// view.addSubview(eventWkWebView)
let myUrl = URL(string: "https://www.google.com")
let myRequest = URLRequest(url: myUrl!)
webView.load(myRequest)
}
}
answered May 3 '18 at 12:43
Gulshan KumarGulshan Kumar
16914
16914
1
the only solution that worked for me iOS 11.4. I have no idea why the other solutions, especially native ones did not work but javascript trick did... Crazy how easy is to develop on Android and how difficult on iOS. Programming should be simple, no?
– undefinedman
Jun 22 '18 at 23:53
add a comment |
1
the only solution that worked for me iOS 11.4. I have no idea why the other solutions, especially native ones did not work but javascript trick did... Crazy how easy is to develop on Android and how difficult on iOS. Programming should be simple, no?
– undefinedman
Jun 22 '18 at 23:53
1
1
the only solution that worked for me iOS 11.4. I have no idea why the other solutions, especially native ones did not work but javascript trick did... Crazy how easy is to develop on Android and how difficult on iOS. Programming should be simple, no?
– undefinedman
Jun 22 '18 at 23:53
the only solution that worked for me iOS 11.4. I have no idea why the other solutions, especially native ones did not work but javascript trick did... Crazy how easy is to develop on Android and how difficult on iOS. Programming should be simple, no?
– undefinedman
Jun 22 '18 at 23:53
add a comment |
In case you display a local html you could just modify this html and put this meta tag to your html:
<head>
<meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='viewport' />
</head>
add a comment |
In case you display a local html you could just modify this html and put this meta tag to your html:
<head>
<meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='viewport' />
</head>
add a comment |
In case you display a local html you could just modify this html and put this meta tag to your html:
<head>
<meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='viewport' />
</head>
In case you display a local html you could just modify this html and put this meta tag to your html:
<head>
<meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='viewport' />
</head>
answered Jul 17 '18 at 14:41
Leszek SzaryLeszek Szary
5,78813532
5,78813532
add a comment |
add a comment |
Swift 2.0
extension WKWebView {
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return nil
}
}
Note that would do it to all WKWebViews
– Luke
Mar 15 '17 at 5:35
1
Not if you subclass it first and apply the extension to the subclass. Works for me.
– BaseZen
Mar 31 '17 at 21:38
1
This doesn't work (anymore?) in iOS 12.
– alexkent
Sep 26 '18 at 12:06
add a comment |
Swift 2.0
extension WKWebView {
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return nil
}
}
Note that would do it to all WKWebViews
– Luke
Mar 15 '17 at 5:35
1
Not if you subclass it first and apply the extension to the subclass. Works for me.
– BaseZen
Mar 31 '17 at 21:38
1
This doesn't work (anymore?) in iOS 12.
– alexkent
Sep 26 '18 at 12:06
add a comment |
Swift 2.0
extension WKWebView {
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return nil
}
}
Swift 2.0
extension WKWebView {
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return nil
}
}
answered Feb 9 '16 at 19:43
![](https://i.stack.imgur.com/HSfNA.jpg?s=32&g=1)
![](https://i.stack.imgur.com/HSfNA.jpg?s=32&g=1)
quemefulquemeful
5,77134051
5,77134051
Note that would do it to all WKWebViews
– Luke
Mar 15 '17 at 5:35
1
Not if you subclass it first and apply the extension to the subclass. Works for me.
– BaseZen
Mar 31 '17 at 21:38
1
This doesn't work (anymore?) in iOS 12.
– alexkent
Sep 26 '18 at 12:06
add a comment |
Note that would do it to all WKWebViews
– Luke
Mar 15 '17 at 5:35
1
Not if you subclass it first and apply the extension to the subclass. Works for me.
– BaseZen
Mar 31 '17 at 21:38
1
This doesn't work (anymore?) in iOS 12.
– alexkent
Sep 26 '18 at 12:06
Note that would do it to all WKWebViews
– Luke
Mar 15 '17 at 5:35
Note that would do it to all WKWebViews
– Luke
Mar 15 '17 at 5:35
1
1
Not if you subclass it first and apply the extension to the subclass. Works for me.
– BaseZen
Mar 31 '17 at 21:38
Not if you subclass it first and apply the extension to the subclass. Works for me.
– BaseZen
Mar 31 '17 at 21:38
1
1
This doesn't work (anymore?) in iOS 12.
– alexkent
Sep 26 '18 at 12:06
This doesn't work (anymore?) in iOS 12.
– alexkent
Sep 26 '18 at 12:06
add a comment |
You can use UIScrollViewDelegate for this. First assign delegate to your webview in viewDidLoad() or any other suitable method as:
class LoginViewController: UIViewController, WKUIDelegate, UIScrollViewDelegate {
override func viewDidLoad() {
webView.scrollView.delegate = self
}
//And add this delegate method in your controller:
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
scrollView.pinchGestureRecognizer?.isEnabled = false
scrollView.panGestureRecognizer.isEnabled = false
}
}
This one worked for me on iOS 12. Other solutions like viewport or return nil for viewForZooming didn't
– Nemanja
Nov 3 '18 at 6:10
add a comment |
You can use UIScrollViewDelegate for this. First assign delegate to your webview in viewDidLoad() or any other suitable method as:
class LoginViewController: UIViewController, WKUIDelegate, UIScrollViewDelegate {
override func viewDidLoad() {
webView.scrollView.delegate = self
}
//And add this delegate method in your controller:
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
scrollView.pinchGestureRecognizer?.isEnabled = false
scrollView.panGestureRecognizer.isEnabled = false
}
}
This one worked for me on iOS 12. Other solutions like viewport or return nil for viewForZooming didn't
– Nemanja
Nov 3 '18 at 6:10
add a comment |
You can use UIScrollViewDelegate for this. First assign delegate to your webview in viewDidLoad() or any other suitable method as:
class LoginViewController: UIViewController, WKUIDelegate, UIScrollViewDelegate {
override func viewDidLoad() {
webView.scrollView.delegate = self
}
//And add this delegate method in your controller:
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
scrollView.pinchGestureRecognizer?.isEnabled = false
scrollView.panGestureRecognizer.isEnabled = false
}
}
You can use UIScrollViewDelegate for this. First assign delegate to your webview in viewDidLoad() or any other suitable method as:
class LoginViewController: UIViewController, WKUIDelegate, UIScrollViewDelegate {
override func viewDidLoad() {
webView.scrollView.delegate = self
}
//And add this delegate method in your controller:
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
scrollView.pinchGestureRecognizer?.isEnabled = false
scrollView.panGestureRecognizer.isEnabled = false
}
}
edited Dec 10 '17 at 14:58
answered Dec 8 '17 at 14:13
Yakup AdYakup Ad
79159
79159
This one worked for me on iOS 12. Other solutions like viewport or return nil for viewForZooming didn't
– Nemanja
Nov 3 '18 at 6:10
add a comment |
This one worked for me on iOS 12. Other solutions like viewport or return nil for viewForZooming didn't
– Nemanja
Nov 3 '18 at 6:10
This one worked for me on iOS 12. Other solutions like viewport or return nil for viewForZooming didn't
– Nemanja
Nov 3 '18 at 6:10
This one worked for me on iOS 12. Other solutions like viewport or return nil for viewForZooming didn't
– Nemanja
Nov 3 '18 at 6:10
add a comment |
this is how I disabled zoom for Swift3 view controller for one-webview-only app
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate, UIScrollViewDelegate {
@IBOutlet var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
webView.scrollView.delegate = self
}
func viewForZooming(in: UIScrollView) -> UIView? {
return nil;
}
}
Finally! Man it's tough tracking down the constant Swift changes (nearly everything else on the web has the delegate func being "viewForZoomingInScrollView". Thanks for this update to "viewForZooming"
– Matt Gardner
Sep 15 '17 at 20:53
@thefaj this solution works fine for me, thats it
– godblessstrawberry
Sep 29 '17 at 8:15
add a comment |
this is how I disabled zoom for Swift3 view controller for one-webview-only app
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate, UIScrollViewDelegate {
@IBOutlet var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
webView.scrollView.delegate = self
}
func viewForZooming(in: UIScrollView) -> UIView? {
return nil;
}
}
Finally! Man it's tough tracking down the constant Swift changes (nearly everything else on the web has the delegate func being "viewForZoomingInScrollView". Thanks for this update to "viewForZooming"
– Matt Gardner
Sep 15 '17 at 20:53
@thefaj this solution works fine for me, thats it
– godblessstrawberry
Sep 29 '17 at 8:15
add a comment |
this is how I disabled zoom for Swift3 view controller for one-webview-only app
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate, UIScrollViewDelegate {
@IBOutlet var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
webView.scrollView.delegate = self
}
func viewForZooming(in: UIScrollView) -> UIView? {
return nil;
}
}
this is how I disabled zoom for Swift3 view controller for one-webview-only app
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate, UIScrollViewDelegate {
@IBOutlet var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
webView.scrollView.delegate = self
}
func viewForZooming(in: UIScrollView) -> UIView? {
return nil;
}
}
answered Aug 18 '17 at 20:47
![](https://i.stack.imgur.com/a3air.png?s=32&g=1)
![](https://i.stack.imgur.com/a3air.png?s=32&g=1)
godblessstrawberrygodblessstrawberry
1,322918
1,322918
Finally! Man it's tough tracking down the constant Swift changes (nearly everything else on the web has the delegate func being "viewForZoomingInScrollView". Thanks for this update to "viewForZooming"
– Matt Gardner
Sep 15 '17 at 20:53
@thefaj this solution works fine for me, thats it
– godblessstrawberry
Sep 29 '17 at 8:15
add a comment |
Finally! Man it's tough tracking down the constant Swift changes (nearly everything else on the web has the delegate func being "viewForZoomingInScrollView". Thanks for this update to "viewForZooming"
– Matt Gardner
Sep 15 '17 at 20:53
@thefaj this solution works fine for me, thats it
– godblessstrawberry
Sep 29 '17 at 8:15
Finally! Man it's tough tracking down the constant Swift changes (nearly everything else on the web has the delegate func being "viewForZoomingInScrollView". Thanks for this update to "viewForZooming"
– Matt Gardner
Sep 15 '17 at 20:53
Finally! Man it's tough tracking down the constant Swift changes (nearly everything else on the web has the delegate func being "viewForZoomingInScrollView". Thanks for this update to "viewForZooming"
– Matt Gardner
Sep 15 '17 at 20:53
@thefaj this solution works fine for me, thats it
– godblessstrawberry
Sep 29 '17 at 8:15
@thefaj this solution works fine for me, thats it
– godblessstrawberry
Sep 29 '17 at 8:15
add a comment |
I don't have enough reputation to add comments to answers, but I wanted to mention that Kevin's solution (meta viewport) no longer works in iOS 10 beta. Landschaft's solution is working for me, though!
Since the JS solution uses W3C standards, it should always be supported.
Ah, Kevin, I wish that were how these things worked.
More here: https://stackoverflow.com/a/37859168/1389714
1
Welp. So it goes, I suppose. Changing the correct answer, thanks for commenting.
– Kevin
Jul 13 '16 at 15:53
add a comment |
I don't have enough reputation to add comments to answers, but I wanted to mention that Kevin's solution (meta viewport) no longer works in iOS 10 beta. Landschaft's solution is working for me, though!
Since the JS solution uses W3C standards, it should always be supported.
Ah, Kevin, I wish that were how these things worked.
More here: https://stackoverflow.com/a/37859168/1389714
1
Welp. So it goes, I suppose. Changing the correct answer, thanks for commenting.
– Kevin
Jul 13 '16 at 15:53
add a comment |
I don't have enough reputation to add comments to answers, but I wanted to mention that Kevin's solution (meta viewport) no longer works in iOS 10 beta. Landschaft's solution is working for me, though!
Since the JS solution uses W3C standards, it should always be supported.
Ah, Kevin, I wish that were how these things worked.
More here: https://stackoverflow.com/a/37859168/1389714
I don't have enough reputation to add comments to answers, but I wanted to mention that Kevin's solution (meta viewport) no longer works in iOS 10 beta. Landschaft's solution is working for me, though!
Since the JS solution uses W3C standards, it should always be supported.
Ah, Kevin, I wish that were how these things worked.
More here: https://stackoverflow.com/a/37859168/1389714
edited May 23 '17 at 12:26
Community♦
11
11
answered Jul 11 '16 at 17:15
markmark
8228
8228
1
Welp. So it goes, I suppose. Changing the correct answer, thanks for commenting.
– Kevin
Jul 13 '16 at 15:53
add a comment |
1
Welp. So it goes, I suppose. Changing the correct answer, thanks for commenting.
– Kevin
Jul 13 '16 at 15:53
1
1
Welp. So it goes, I suppose. Changing the correct answer, thanks for commenting.
– Kevin
Jul 13 '16 at 15:53
Welp. So it goes, I suppose. Changing the correct answer, thanks for commenting.
– Kevin
Jul 13 '16 at 15:53
add a comment |
If it's not important for you to handle links inside html (say you want to display text only) the simplest would be to turn off user interaction
webView.userInteractionEnabled = false
This will disable url tapping
– Timur Bernikovich
Jan 30 '17 at 12:39
Hence I wrote "If it's not important for you to handle links inside html "
– lvp
Jan 30 '17 at 12:41
This will disable scrolling! The OP wants to the disable pinch to zoom but I guess he wants a usable webview.
– tanzolone
Mar 6 '17 at 18:02
add a comment |
If it's not important for you to handle links inside html (say you want to display text only) the simplest would be to turn off user interaction
webView.userInteractionEnabled = false
This will disable url tapping
– Timur Bernikovich
Jan 30 '17 at 12:39
Hence I wrote "If it's not important for you to handle links inside html "
– lvp
Jan 30 '17 at 12:41
This will disable scrolling! The OP wants to the disable pinch to zoom but I guess he wants a usable webview.
– tanzolone
Mar 6 '17 at 18:02
add a comment |
If it's not important for you to handle links inside html (say you want to display text only) the simplest would be to turn off user interaction
webView.userInteractionEnabled = false
If it's not important for you to handle links inside html (say you want to display text only) the simplest would be to turn off user interaction
webView.userInteractionEnabled = false
edited Oct 6 '16 at 15:02
answered Jul 13 '16 at 9:36
lvplvp
1,7821521
1,7821521
This will disable url tapping
– Timur Bernikovich
Jan 30 '17 at 12:39
Hence I wrote "If it's not important for you to handle links inside html "
– lvp
Jan 30 '17 at 12:41
This will disable scrolling! The OP wants to the disable pinch to zoom but I guess he wants a usable webview.
– tanzolone
Mar 6 '17 at 18:02
add a comment |
This will disable url tapping
– Timur Bernikovich
Jan 30 '17 at 12:39
Hence I wrote "If it's not important for you to handle links inside html "
– lvp
Jan 30 '17 at 12:41
This will disable scrolling! The OP wants to the disable pinch to zoom but I guess he wants a usable webview.
– tanzolone
Mar 6 '17 at 18:02
This will disable url tapping
– Timur Bernikovich
Jan 30 '17 at 12:39
This will disable url tapping
– Timur Bernikovich
Jan 30 '17 at 12:39
Hence I wrote "If it's not important for you to handle links inside html "
– lvp
Jan 30 '17 at 12:41
Hence I wrote "If it's not important for you to handle links inside html "
– lvp
Jan 30 '17 at 12:41
This will disable scrolling! The OP wants to the disable pinch to zoom but I guess he wants a usable webview.
– tanzolone
Mar 6 '17 at 18:02
This will disable scrolling! The OP wants to the disable pinch to zoom but I guess he wants a usable webview.
– tanzolone
Mar 6 '17 at 18:02
add a comment |
Disable double tap to zoom gesture by require failure of gesture recognizer. It will prevent the browser to take action when user double tap.
import UIKit
class DisableDoubleTapRecognizer : UITapGestureRecognizer, UIGestureRecognizerDelegate{
override init(target: Any?, action: Selector?) {
super.init(target: target, action: action)
}
init() {
super.init(target:nil, action: nil)
self.numberOfTapsRequired = 2;
self.delegate = self;
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true;
}
}
//in your view controller
override func viewDidLoad() {
super.viewDidLoad()
webView.addGestureRecognizer(DisableDoubleTapRecognizer())
}
add a comment |
Disable double tap to zoom gesture by require failure of gesture recognizer. It will prevent the browser to take action when user double tap.
import UIKit
class DisableDoubleTapRecognizer : UITapGestureRecognizer, UIGestureRecognizerDelegate{
override init(target: Any?, action: Selector?) {
super.init(target: target, action: action)
}
init() {
super.init(target:nil, action: nil)
self.numberOfTapsRequired = 2;
self.delegate = self;
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true;
}
}
//in your view controller
override func viewDidLoad() {
super.viewDidLoad()
webView.addGestureRecognizer(DisableDoubleTapRecognizer())
}
add a comment |
Disable double tap to zoom gesture by require failure of gesture recognizer. It will prevent the browser to take action when user double tap.
import UIKit
class DisableDoubleTapRecognizer : UITapGestureRecognizer, UIGestureRecognizerDelegate{
override init(target: Any?, action: Selector?) {
super.init(target: target, action: action)
}
init() {
super.init(target:nil, action: nil)
self.numberOfTapsRequired = 2;
self.delegate = self;
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true;
}
}
//in your view controller
override func viewDidLoad() {
super.viewDidLoad()
webView.addGestureRecognizer(DisableDoubleTapRecognizer())
}
Disable double tap to zoom gesture by require failure of gesture recognizer. It will prevent the browser to take action when user double tap.
import UIKit
class DisableDoubleTapRecognizer : UITapGestureRecognizer, UIGestureRecognizerDelegate{
override init(target: Any?, action: Selector?) {
super.init(target: target, action: action)
}
init() {
super.init(target:nil, action: nil)
self.numberOfTapsRequired = 2;
self.delegate = self;
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true;
}
}
//in your view controller
override func viewDidLoad() {
super.viewDidLoad()
webView.addGestureRecognizer(DisableDoubleTapRecognizer())
}
import UIKit
class DisableDoubleTapRecognizer : UITapGestureRecognizer, UIGestureRecognizerDelegate{
override init(target: Any?, action: Selector?) {
super.init(target: target, action: action)
}
init() {
super.init(target:nil, action: nil)
self.numberOfTapsRequired = 2;
self.delegate = self;
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true;
}
}
//in your view controller
override func viewDidLoad() {
super.viewDidLoad()
webView.addGestureRecognizer(DisableDoubleTapRecognizer())
}
import UIKit
class DisableDoubleTapRecognizer : UITapGestureRecognizer, UIGestureRecognizerDelegate{
override init(target: Any?, action: Selector?) {
super.init(target: target, action: action)
}
init() {
super.init(target:nil, action: nil)
self.numberOfTapsRequired = 2;
self.delegate = self;
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true;
}
}
//in your view controller
override func viewDidLoad() {
super.viewDidLoad()
webView.addGestureRecognizer(DisableDoubleTapRecognizer())
}
answered May 18 '18 at 10:04
user3543806user3543806
111
111
add a comment |
add a comment |
We need to change the delegate call with following signatures in XCode 8:
override func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return nil
}
add a comment |
We need to change the delegate call with following signatures in XCode 8:
override func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return nil
}
add a comment |
We need to change the delegate call with following signatures in XCode 8:
override func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return nil
}
We need to change the delegate call with following signatures in XCode 8:
override func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return nil
}
answered Dec 9 '16 at 0:13
![](https://lh3.googleusercontent.com/-fIRuypSjV0s/AAAAAAAAAAI/AAAAAAAAE3k/_9LVKJxkgJg/photo.jpg?sz=32)
![](https://lh3.googleusercontent.com/-fIRuypSjV0s/AAAAAAAAAAI/AAAAAAAAE3k/_9LVKJxkgJg/photo.jpg?sz=32)
BriansterBrianster
91
91
add a comment |
add a comment |
If the webview is user for read-only purposes, i.e, just for displaying the web content use
webView.isUserInteractionEnabled = false
By this the zoom gesture won't work on it.
This will also disable scrolling. Just because the content is read-only does not mean you cannot scroll through it.
– Bassem Sameh
May 18 '18 at 11:04
add a comment |
If the webview is user for read-only purposes, i.e, just for displaying the web content use
webView.isUserInteractionEnabled = false
By this the zoom gesture won't work on it.
This will also disable scrolling. Just because the content is read-only does not mean you cannot scroll through it.
– Bassem Sameh
May 18 '18 at 11:04
add a comment |
If the webview is user for read-only purposes, i.e, just for displaying the web content use
webView.isUserInteractionEnabled = false
By this the zoom gesture won't work on it.
If the webview is user for read-only purposes, i.e, just for displaying the web content use
webView.isUserInteractionEnabled = false
By this the zoom gesture won't work on it.
answered Dec 5 '17 at 8:39
Sameer BhideSameer Bhide
1276
1276
This will also disable scrolling. Just because the content is read-only does not mean you cannot scroll through it.
– Bassem Sameh
May 18 '18 at 11:04
add a comment |
This will also disable scrolling. Just because the content is read-only does not mean you cannot scroll through it.
– Bassem Sameh
May 18 '18 at 11:04
This will also disable scrolling. Just because the content is read-only does not mean you cannot scroll through it.
– Bassem Sameh
May 18 '18 at 11:04
This will also disable scrolling. Just because the content is read-only does not mean you cannot scroll through it.
– Bassem Sameh
May 18 '18 at 11:04
add a comment |
Here's a slightly modified version of Gulshan Kumar's answer that worked for me in iOS 12.1.2, and it also prevents zooming due to double-taps and rotation. In my example, I just inject the script directly into the web view. I adjusted the scale to 60%, and there's no need to use concatenation in building up the source string.
let source = "var meta = document.createElement('meta'); meta.name = 'viewport'; meta.content = 'width=device-width, initial-scale=0.6, maximum-scale=0.6, user-scalable=no'; var head = document.getElementsByTagName('head')[0]; head.appendChild(meta);"
let script = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
webView.configuration.userContentController.addUserScript(script)
add a comment |
Here's a slightly modified version of Gulshan Kumar's answer that worked for me in iOS 12.1.2, and it also prevents zooming due to double-taps and rotation. In my example, I just inject the script directly into the web view. I adjusted the scale to 60%, and there's no need to use concatenation in building up the source string.
let source = "var meta = document.createElement('meta'); meta.name = 'viewport'; meta.content = 'width=device-width, initial-scale=0.6, maximum-scale=0.6, user-scalable=no'; var head = document.getElementsByTagName('head')[0]; head.appendChild(meta);"
let script = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
webView.configuration.userContentController.addUserScript(script)
add a comment |
Here's a slightly modified version of Gulshan Kumar's answer that worked for me in iOS 12.1.2, and it also prevents zooming due to double-taps and rotation. In my example, I just inject the script directly into the web view. I adjusted the scale to 60%, and there's no need to use concatenation in building up the source string.
let source = "var meta = document.createElement('meta'); meta.name = 'viewport'; meta.content = 'width=device-width, initial-scale=0.6, maximum-scale=0.6, user-scalable=no'; var head = document.getElementsByTagName('head')[0]; head.appendChild(meta);"
let script = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
webView.configuration.userContentController.addUserScript(script)
Here's a slightly modified version of Gulshan Kumar's answer that worked for me in iOS 12.1.2, and it also prevents zooming due to double-taps and rotation. In my example, I just inject the script directly into the web view. I adjusted the scale to 60%, and there's no need to use concatenation in building up the source string.
let source = "var meta = document.createElement('meta'); meta.name = 'viewport'; meta.content = 'width=device-width, initial-scale=0.6, maximum-scale=0.6, user-scalable=no'; var head = document.getElementsByTagName('head')[0]; head.appendChild(meta);"
let script = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
webView.configuration.userContentController.addUserScript(script)
answered Dec 31 '18 at 18:45
Scott GardnerScott Gardner
6,65512928
6,65512928
add a comment |
add a comment |
The application I work on needed the view to be zoomed by Javascript. The accepted answer blocked zooming by JavaScript from inside the page too.
I only needed to disable pinch gesture by the user of the appliction. The only solution I've found is to disable the gesture of the web view after the page has loaded:
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
/* disable pinch gesture recognizer to allow zooming the web view by code but prevent zooming it by the user */
for (UIGestureRecognizer *gr in self.webView.scrollView.gestureRecognizers) {
if ([gr isKindOfClass:[UIPinchGestureRecognizer class]]) {
gr.enabled = NO;
}
}
}
add a comment |
The application I work on needed the view to be zoomed by Javascript. The accepted answer blocked zooming by JavaScript from inside the page too.
I only needed to disable pinch gesture by the user of the appliction. The only solution I've found is to disable the gesture of the web view after the page has loaded:
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
/* disable pinch gesture recognizer to allow zooming the web view by code but prevent zooming it by the user */
for (UIGestureRecognizer *gr in self.webView.scrollView.gestureRecognizers) {
if ([gr isKindOfClass:[UIPinchGestureRecognizer class]]) {
gr.enabled = NO;
}
}
}
add a comment |
The application I work on needed the view to be zoomed by Javascript. The accepted answer blocked zooming by JavaScript from inside the page too.
I only needed to disable pinch gesture by the user of the appliction. The only solution I've found is to disable the gesture of the web view after the page has loaded:
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
/* disable pinch gesture recognizer to allow zooming the web view by code but prevent zooming it by the user */
for (UIGestureRecognizer *gr in self.webView.scrollView.gestureRecognizers) {
if ([gr isKindOfClass:[UIPinchGestureRecognizer class]]) {
gr.enabled = NO;
}
}
}
The application I work on needed the view to be zoomed by Javascript. The accepted answer blocked zooming by JavaScript from inside the page too.
I only needed to disable pinch gesture by the user of the appliction. The only solution I've found is to disable the gesture of the web view after the page has loaded:
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
/* disable pinch gesture recognizer to allow zooming the web view by code but prevent zooming it by the user */
for (UIGestureRecognizer *gr in self.webView.scrollView.gestureRecognizers) {
if ([gr isKindOfClass:[UIPinchGestureRecognizer class]]) {
gr.enabled = NO;
}
}
}
answered Feb 23 '17 at 9:44
UrKUrK
1,04721832
1,04721832
add a comment |
add a comment |
This worked for me. https://gist.github.com/paulofierro/5b642dcde5ee9e86a130
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)
add a comment |
This worked for me. https://gist.github.com/paulofierro/5b642dcde5ee9e86a130
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)
add a comment |
This worked for me. https://gist.github.com/paulofierro/5b642dcde5ee9e86a130
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)
This worked for me. https://gist.github.com/paulofierro/5b642dcde5ee9e86a130
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)
answered Apr 19 '18 at 2:43
JonnyJonny
9,275885182
9,275885182
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25553711%2fdisable-magnification-gesture-in-wkwebview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5CWvsjgg HfUyFhXENQ,GNFV1fx7p4AxtzIRq