Melina
Melina

Reputation: 1463

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?

Upvotes: 6

Views: 5116

Answers (4)

GurPreet_Singh
GurPreet_Singh

Reputation: 386

No need to use complex methods. You can access Scrollview of webview directly as below.

web_view.scrollView.scrollEnabled = NO;

Upvotes: 0

user792896
user792896

Reputation:

You can also access the scrollView like this :

webView.scrollView.scrollEnabled = false;
webView.scrollView.bounces = false;

Upvotes: 2

Max
Max

Reputation: 1060

Using @aaron-saunders and @matt-rix 's answers, here's what works best for me :

UIView *v = [[webView subviews] lastObject];
if([v isKindOfClass:[UIScrollView class]])
    [v setScrollEnabled:NO];

Upvotes: 1

Aaron Saunders
Aaron Saunders

Reputation: 33345

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];
    }
}

Upvotes: 7

Related Questions