How to disable scrolling entirely in a WKWebView?
Took me a while but I figured out a way of doing this.
I had to remove a private gesture recognizer within a private subview of the WKWebView
.
I had a category on WKWebView
to do so:
@implementation WKWebView (Scrolling)- (void)setScrollEnabled:(BOOL)enabled { self.scrollView.scrollEnabled = enabled; self.scrollView.panGestureRecognizer.enabled = enabled; self.scrollView.bounces = enabled; // There is one subview as of iOS 8.1 of class WKScrollView for (UIView* subview in self.subviews) { if ([subview respondsToSelector:@selector(setScrollEnabled:)]) { [(id)subview setScrollEnabled:enabled]; } if ([subview respondsToSelector:@selector(setBounces:)]) { [(id)subview setBounces:enabled]; } if ([subview respondsToSelector:@selector(panGestureRecognizer)]) { [[(id)subview panGestureRecognizer] setEnabled:enabled]; } // here comes the tricky part, desabling for (UIView* subScrollView in subview.subviews) { if ([subScrollView isKindOfClass:NSClassFromString(@"WKContentView")]) { for (id gesture in [subScrollView gestureRecognizers]) { if ([gesture isKindOfClass:NSClassFromString(@"UIWebTouchEventsGestureRecognizer")]) [subScrollView removeGestureRecognizer:gesture]; } } } }}@end
Hope this helps anyone some day.
Credit and many thanks to apouche for the Obj-C code. In case anybody else has the same problem, here is the working solution adapted for Swift 2
extension WKWebView { func setScrollEnabled(enabled: Bool) { self.scrollView.scrollEnabled = enabled self.scrollView.panGestureRecognizer.enabled = enabled self.scrollView.bounces = enabled for subview in self.subviews { if let subview = subview as? UIScrollView { subview.scrollEnabled = enabled subview.bounces = enabled subview.panGestureRecognizer.enabled = enabled } for subScrollView in subview.subviews { if subScrollView.dynamicType == NSClassFromString("WKContentView")! { for gesture in subScrollView.gestureRecognizers! { subScrollView.removeGestureRecognizer(gesture) } } } } }}