How to disable UIWebView from user scrolling?

#import <objc/runtime.h>

    id scroller = [[Webview subviews] lastObject];
    int count;
    Method *method = class_copyMethodList([scroller class], &count);
    int i;
    for (i=0; i<count; i++) {
        if (strcmp(method_getName(method[i]), "setScrollingEnabled:") == 0)
            break;
    }
    IMP test = method_getImplementation(method[i]);
    test(scroller, @selector(setScrollingEnabled:), NO);

Blogged with the Flock Browser

9 Responses to “How to disable UIWebView from user scrolling?”

  1. wow! Says:

    incredible!

  2. slaing Says:

    Don’t forget to free(method) or you’ll have a leak on your hands.

  3. uh Says:

    or you could just do :
    [[[WebView subviews] lastObject] setScrollingEnabled:NO];

    this is objective-c, you know :)

  4. Hussain Says:

    not working for 3.0 i guess. it just gives me no such method found.

    There is another method called setScrollEnabled but thats giving error

  5. Georg Says:

    id scrollView = [[self subviews] lastObject];
    if([scrollView respondsToSelector:@selector(setScrollingEnabled:)]) [scrollView performSelector:@selector(setScrollingEnabled:) withObject:NO];

  6. Cam Says:

    While this is a great solution and works perfectly, it uses private API’s which will get your App rejected. Mine just did for this very reason.

  7. Georg Says:

    Found something that shouldn’t get rejected:

    NSString *preventScrolling = @”document.onload = function(){document.ontouchmove = function(e){e.preventDefault();}};”;
    [webView stringByEvaluatingJavaScriptFromString:preventScrolling];

  8. Ricardo Says:

    What did they tell you. How can you make it prevent from scrolling? The method Georg proposes disables other things.


Leave a Reply