2015-02-20 25 views
8

So che questo sembra una semplice domanda si può semplicemente dire:Come disabilitare completamente lo scorrimento in WKWebView?

webview.scrollView.scrollEnabled = NO; 
webview.scrollView.panGestureRecognizer.enabled = NO; 
webview.scrollView.bounces = NO; 

o anche

for (UIView* subview in webview.subviews) { 
    if ([subview respondsToSelector:@selector(setScrollEnabled:)]) { 
     [(id)subview setScrollEnabled:enabled]; 
    } 

    if ([subview respondsToSelector:@selector(panGestureRecognizer)]) { 
     [[(id)subview panGestureRecognizer] setEnabled:enabled]; 
    } 
} 

ma mentre lo fa evitare scolling (nel senso contentOffset) all'interno del WKWebview è doesn' t impediscono la ricezione di eventi di movimento panoramico che comportano lo scorrimento.

Quindi articoli come quelli di Huffington Post, che hanno incluso javascript per cambiare automaticamente gli articoli quando l'utente scorre a sinistra oa destra continua a ottenere quel comportamento.

Come posso evitare questo?

risposta

0

tenta di disattivare ScrollView zoom in questo modo:

CGFloat zoomScale = webview.scrollView.zoomScale; 
webview.scrollView.maximumZoomScale = zoomScale; 
webview.scrollView.minimumZoomScale = zoomScale; 
4

ho messo un po ', ma ho trovato un modo di fare questo.

Ho dovuto rimuovere un riconoscimento di gesto privato all'interno di una sottoview privata di WKWebView.

ho avuto una categoria a WKWebView per farlo:

@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 

Spero che questo aiuti qualcuno un giorno.

0

Ecco una versione rapida se qualcuno ha ancora dei problemi con questo problema

let subviews = self.theWebView.scrollView.subviews 
    for subview in subviews{ 
     if(subview.isKindOfClass(NSClassFromString("WKContentView"))){ 
       if let recognizers = subview.gestureRecognizers { 
        for recognizer in recognizers! { 
         if recognizer.isKindOfClass(NSClassFromString("UIWebTouchEventsGestureRecognizer")){ 
          subview.removeGestureRecognizer(recognizer as! UIGestureRecognizer) 
         } 
        } 
       } 
      } 
     } 
2

Credito e molte grazie per apouche per il codice Obj-C. Nel caso qualcuno altro ha lo stesso problema, ecco la soluzione di lavoro adatto per 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) 
       } 
      } 
     } 
    } 
    } 
} 
18

è possibile scorrere semplicemente disabilitare sulla sua implicita ScrollView

webView.scrollView.scrollEnabled = false 

E funziona. Voila ...

+3

In Swift 3 questo ha funzionato per me: 'webView.scrollView.isScrollEnabled = false' – Jonathan

0

finalmente self.webView.scrollView.userInteractionEnabled = NO

+0

Evita anche eventi di tocco come il clic. –

0

Ecco una versione Swift 3:

extension WKWebView { 

    func setScrollEnabled(enabled: Bool) { 
     self.scrollView.isScrollEnabled = enabled 
     self.scrollView.panGestureRecognizer.isEnabled = enabled 
     self.scrollView.bounces = enabled 

     for subview in self.subviews { 
      if let subview = subview as? UIScrollView { 
       subview.isScrollEnabled = enabled 
       subview.bounces = enabled 
       subview.panGestureRecognizer.isEnabled = enabled 
      } 

      for subScrollView in subview.subviews { 
       if type(of: subScrollView) == NSClassFromString("WKContentView")! { 
        for gesture in subScrollView.gestureRecognizers! { 
         subScrollView.removeGestureRecognizer(gesture) 
        } 
       } 
      } 
     } 
    } 
} 
Problemi correlati