94

I have to show a web article with a UITableView under the article.

The only option I found was to display the article in a UIWebView in the tableView header.

In order to do that I have to get the height of the webView content and I have to disable scrolling for the webView.

I found two solutions to disable scrolling:

for (id subview in webView.subviews)
    if ([[subview class] isSubclassOfClass: [UIScrollView class]])
        ((UIScrollView *)subview).scrollEnabled=NO;

or in JavaScript:

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

I heard that the first solution is forbidden by Apple but I don't have any proof of it. Will my application be rejected by using this solution? If so, can I use the second solution without being rejected?

Cœur
  • 37,241
  • 25
  • 195
  • 267
jsaddour
  • 941
  • 1
  • 6
  • 3

18 Answers18

302

Starting with iOS5 we have direct access to the scrollview of the UIWebView.
You can disable scrolling and bounces like this:

webView.scrollView.scrollEnabled = NO; 
webView.scrollView.bounces = NO;
Alex Stanciu
  • 5,183
  • 2
  • 24
  • 31
  • For some reason, this is not working for me. :-( I have put a ScrollView and have put a UIWebView in it. Then set the above 2 lines in viewWillAppear method. But the scroll still works in web view. Can someone help?? Just for information, I'm running it in iOS 7 simulator. – Amogh Natu May 06 '14 at 07:24
  • what's the use of `webView.scrollView.bounces = NO;`? since the code in the first line has disabled the scrolling. – Gon Nov 27 '14 at 12:32
  • 1
    Love it when people take an answer and run without taking the time to give the contributor an up vote. Is there no way to notify the user to please choose an answer to vote ? – John Cogan Feb 25 '16 at 17:20
  • 1
    for swift webView.scrollView.scrollEnabled = false / webView.scrollView.bounces = false – Mudith Chathuranga Silva Apr 26 '16 at 04:41
  • only `webView.scrollView.scrollEnabled = NO; ` worked for me, no need to implement `webView.scrollView.bounces = NO;` – Mohammad Zaid Pathan Feb 24 '17 at 05:45
  • Swift 4 version: webView.scrollView.isScrollEnabled = false – Carioni May 25 '19 at 13:06
26

Since this question is still applicable, there is a new way to do it! (iOS 7.0+, perhaps iOS 6 also, not confirmed)

webView.scrollView.scrollEnabled = NO;

Take care :)

Ryan Copley
  • 873
  • 1
  • 10
  • 26
20

You can simply set the web view user interaction property to no i.e.

webView.userInteractionEnabled = NO;

Its pretty simple and works fine,thanks :)

Eshwar Chaitanya
  • 697
  • 10
  • 21
12

for all ios versions you can use this code instead of setScrollEnabled :

for (UIView *view in webview.subviews) {
    if ([view isKindOfClass:[UIScrollView class]]) {
       UIScrollView *scrollView = (UIScrollView *)view;
       scrollView.scrollEnabled = NO;
    }
}
mturhan55
  • 211
  • 3
  • 9
  • firstly, i try "setScrollEnabled" method and apple rejected my ipa. Then i changed this method and accepted. – mturhan55 Aug 13 '12 at 13:42
11

Use this one <body ontouchmove="event.preventDefault()">

Gobi M
  • 3,243
  • 5
  • 32
  • 47
11

SWIFT 3 version

    webView.scrollView.bounces = false
    webView.scrollView.isScrollEnabled = false
Ahmed Safadi
  • 4,402
  • 37
  • 33
7

I think the Javascript

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

is far the best solution.

Indeed :

Your first examle, or the

[[[WebView subviews] lastObject] setScrollingEnabled:NO];

one (same things) could be rejected because not documented, or make you app crash in a further ios update.

And the

[[myWebView scrollView] setBounces:NO];

method won't work with iOS < 5.

Martin
  • 11,881
  • 6
  • 64
  • 110
5
[[[WebView subviews] lastObject] setScrollingEnabled:NO];

that sorted it for me

theiOSDude
  • 1,480
  • 2
  • 21
  • 36
  • 4
    Would be so wrong to rely on this as it is not an exposed API. – Deepak Danduprolu Jun 22 '11 at 09:51
  • [[[WebView subviews] lastObject] setScrollingEnabled:NO]; that sorted it for me. Anyway, it doesn't answer my question. My question is I'm a allowed to do that? It seems that disabling scroll through the SDK is forbidden. But I'm not sure. And what about disabling scroll through javascript. – jsaddour Jun 22 '11 at 09:56
  • there is no evidence that my suggestion would cause a rejection by apple, infact, by calling the last subiew in the subviews array and setting scrollingEnabled against it is exposed API? surely?. and yeah go for it cause you can use it. no idea via javascript though. sorry. – theiOSDude Jun 22 '11 at 11:41
  • 3
    No, you should not do this. As Deepak says, you can't rely on the undocumented internal order of subviews in one of Apple's UI elements. If they change the internal structure in a future OS update, your application will crash. They specifically warn about doing this in multiple places, and many applications broke in the upgrade to iPhone OS 3.0 just because they did this. – Brad Larson Sep 29 '11 at 00:58
5

The first is rejected by Apple. It happened to me just this morning.

Jane Sales
  • 13,526
  • 3
  • 52
  • 57
5

[[myWebView scrollView] setBounces:NO];

mafonya
  • 2,152
  • 22
  • 21
4

Use it :

[[webView scrollView] setBounces:NO];
[[webView scrollView] setScrollingEnabled:NO];
4

I don't think the first option is forbidden by Apple.

You could try

[[[Webview subviews] lastObject] setScrollingEnabled:NO];

If you don't want any links to work in your UIWebView, you can also do

[myWebView setUserInteractionEnabled:NO];
Nick Bull
  • 4,276
  • 1
  • 18
  • 25
3

Try this,

[(UIScrollView *)[[webView subviews] lastObject] setScrollEnabled:NO];    
Shanmugaraja G
  • 2,778
  • 4
  • 31
  • 47
3

I placed some UIWebViews inside a UIScrollView and it wasn't scrolling for me, unless I disable the user interaction on the webviews (even with the [[webview scrollview] setScrollEnabled:NO]). So I placed the UIWebViews inside a UIView and that worked for me but then the interactive links or divs in my HTML inside the UIWebView were not working anymore.

The only thing that worked for me is to keep the UIWebView inside the UIScrollView so the HTML interactions work but the UIScrolling does not.

I then created a custom delegate for the webviews to listen for window.open("http:/customEvent") that is called from the HTML contained in the webview. I also added a JavaScript swipe detection that will fire up my custom event.

When the webview delegate receives this, I pass a notification to a controller which then dynamically scrolls my UIScrollView. Obviously I had to build a logic to monitor and scroll to the next or previous page. Not the prettiest solution but It's working for me.

Emil
  • 7,220
  • 17
  • 76
  • 135
Mark
  • 31
  • 1
3

Try this in your HTML (<HEAD> tag)

<script> 
  document.ontouchmove = function(event) { event.preventDefault(); }
</script>

It will disable scrolling on your web page.

(works on any webpage, not just UIWebView)

Gik
  • 528
  • 5
  • 18
2

I needed to disable scrolling because dismissing a custom keyboard really messed up scrolling with the webView. Nothing else worked and I resorted to something like this:

-(void)viewDidLoad{
  [super viewDidLoad];
  [self.webView.scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
  self.webView.scrollView.showsVerticalScrollIndicator = NO;
}  

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if (!CGPointEqualToPoint(self.webView.scrollView.contentOffset, CGPointZero)){
        self.contentOffset = CGPointZero;
    }
}
akiraspeirs
  • 2,127
  • 2
  • 21
  • 29
1

Is a UIWebview and a UITableView on a UIScrollview, and setting the height of the webview (and adding to the total ContentSize of the Scrollview) like what you want?

Henrik Erlandsson
  • 3,797
  • 5
  • 43
  • 63
  • Almost. But it seems less complicated to display the WebView in the tableView header. But my question Is: I'am allowed to disable webView scroll through SDK? If not I am allowed to do taht trough javascript? – jsaddour Jun 22 '11 at 10:02
1
[[[WebView subviews] lastObject] setScrollingEnabled:NO];
Marek Grzenkowicz
  • 17,024
  • 9
  • 81
  • 111