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
About these ads

22 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 :)

    • Christine Says:

      Thanks for the post. This was v helpful!

    • AP Says:

      I wanted to try this but did not work. Just a little mistake from OP but if someone needs this solution:

      [[[yourUIWebView subviews] lastObject] setScrollEnabled:NO];

  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.

  9. ashish Says:

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

  10. Tobin Says:

    Give this a shot –

    UIScrollView *scrollView = [[myWebView subviews] lastObject];
    scrollView.scrollEnabled = FALSE;

    This fixes the problem, while also keeping the links click-able, any scrollViews it’s embedded in scrollable, and uses only public API’s.

  11. MHHagan Says:

    UIScrollView *scrollView = [[myWebView subviews] lastObject];
    scrollView.scrollEnabled = FALSE;

    I like this! Is this legit? Can anyone confirm?

    • Gideon Says:

      Nope… Just got a app rejected because of this…

      • Louis dd Says:

        the code works, now I’m surprised you got rejected for that.
        What was apple’s argument for doing that ?

    • Steve Says:

      To avoid getting rejected by Apple, you may want to avoid assuming that the last view is the scroll-view. Instead, do something like this:

      UIView* pRow = nil;
      for(pRow in webView.subviews){
      if([pRow isKindOfClass:[UIScrollView class] ]){
      UIScrollView* pScrollRow = (UIScrollView*) pRow;
      pScrollRow.scrollEnabled = NO;
      pScrollRow.bounces = NO;
      }
      }

      I found this solution at http://techcussion.com/?p=32 .

  12. 2010 in review « TEchnical SOlutions Says:

    […] The busiest day of the year was December 2nd with 155 views. The most popular post that day was How to disable UIWebView from user scrolling?. […]

  13. mato Says:

    How To Disable Scrolling In A UIWebView ios 4 where i have to put [[[webview subviews] lastObject] setScrollEnabled:NO ?

    please help me

    #import “ActivityIndicatorViewController.h”

    @implementation ActivityIndicatorViewController
    @synthesize webview;
    @synthesize activity;

    /*
    // The designated initializer. Override to perform setup that is required before the view is loaded.
    – (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
    // Custom initialization
    }
    return self;
    }
    */

    /*
    // Implement loadView to create a view hierarchy programmatically, without using a nib.
    – (void)loadView {
    }
    */

    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    – (void)viewDidLoad {
    [super viewDidLoad];

    timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(tick) userInfo:nil repeats:YES];

    webview.scalesPageToFit = YES;

    webview.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@”http://www.apple.com”]]];

    }

    -(void)tick {
    if(!webview.loading)
    [activity stopAnimating];
    else
    [activity startAnimating];

    }

    // Override to allow orientations other than the default portrait orientation.
    – (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (YES);
    }

    – (void)didReceiveMemoryWarning {
    // Releases the view if it doesn’t have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren’t in use.
    }

    – (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    }

    – (void)dealloc {
    [super dealloc];
    }

    @end

  14. Martin Says:

    @mato

    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    – (void)viewDidLoad {
    [super viewDidLoad];

    [[[webview subviews] lastObject] setScrollEnabled:NO];

    timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(tick) userInfo:nil repeats:YES];

    webview.scalesPageToFit = YES;

    webview.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@”http://www.apple.com”]]];

    }

  15. Vishal Says:

    This Code Disable the Scrolling but if i want disable only Vertical scrollview than is it possible ?
    Any Solution… :-)

  16. raidu Says:

    i am xcode4.0 the above solution is not working for me.. can you tell me any alternate solution.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 357 other followers

%d bloggers like this: