Tuesday, April 5, 2011

Disable UIWebView default scrolling behavior using objective-c

I know you can use a javascript to do this

<script type="text/javascript">
touchMove = function(event) {
event.preventDefault();
}

Is there a way to do the same using objective-c?

From stackoverflow
  • try this...

    UIView * v = [[webView subviews] lastObject];
    [v setScrollEnabled:NO];
    [v bounces:NO];
    

    EDIT: Added checks to original answer based on comment below

    UIView * v = [[webView subviews] lastObject];
    if([v isKindOfClass:[UIScrollView class] ]) {
        if ([v respondsToSelector:@selector(setScrollEnabled]) {
            [v setScrollEnabled:NO];
        }
        if ([v respondsToSelector:@selector(bounces)]) {
            [v bounces:NO];
        }
    }
    
    rpetrich : This is a very bad idea. `UIWebView`'s internal view hierarchy can change at any OS update. At least check to see if the subview responds to those methods before calling them.
    Melina : can you explain what this means? is lastObject the element?
    Aaron Saunders : @Melina the last subview of the UIWebView, @rpetrich added the check, thx
    Melina : the last subview of UIWebView is the actual content area? right?

0 comments:

Post a Comment