How to turn off UIWebView horizontal bounce
Asked Answered
T

2

5

Simple problem, I have a webview that is supposed to hold just an image for the user to be able to zoom in and out. To keep my look clean, I want to completely disable bouncing on this view, but still allow scrolling. This solution does work for the vertical bounce, but as soon as I zoom the image to a size larger than the screen, horizontal bounce is still possible:

for (id subview in webView.subviews 
{
    if ( [[subview class] isSubclassOfClass:[UIScrollView class]] )
    {

        ((UIScrollView*) subview).bounces = NO;
        ((UIScrollView*) subview).alwaysBounceVertical = NO;
        ((UIScrollView*) subview).alwaysBounceHorizontal = NO;
        ((UIScrollView*) subview).bouncesZoom = NO;            
    }
}
Tula answered 13/5, 2011 at 18:5 Comment(0)
A
11

The following code did the trick for us to stop bouncing:

NSString* scriptToPreventBouncing = @"<script type=\"text/javascript\"> document.ontouchmove = function(e){ e.preventDefault(); } </script>";
NSString* footerHTML = @"<div>All rights reserved</div>";
[footer loadHTMLString: [scriptToPreventBouncing stringByAppendingString:footerHTML] baseURL:[NSURL URLWithString:@"http://somewebsite.com"]];

I am not sure if this will disable the zooming of image. But it should stop the bouncing.

Aguirre answered 13/5, 2011 at 18:12 Comment(4)
That did stop the zooming and scrolling entirelyTula
worked great! Now I'm just writing because 2 words aren't enough for stack overflow to like that I like your answer. ;)Mercorr
This solution is really simple! It deserves a lot of reputation points!Varletry
Could one of you explain this a little more ? Where should I put this? - and what are the other parts besides the script for? Why is there a div involved? Thank you.Wessling
C
7

On iOS 5 you can solve your problem this way:

UIWebView *webView = [[UIWebView alloc] ....];

webView.scrollView.scrollEnabled = NO; 
webView.scrollView.bounces = NO;

NOTE: this will work only on iOS5 and higher

Cretan answered 25/1, 2012 at 15:48 Comment(2)
For me, this does not prevent horizontal bounce.Washery
Correction, this was only for Twitter.comWashery

© 2022 - 2024 — McMap. All rights reserved.