1

I have placed following javascript in my html file.

<script TYPE="text/javascript">
    function srk(){
        document.ontouchmove = function(e){ 
            e.preventDefault(); 
        }
    }
</script>

I am scrolling my webview by following code with some animation.

[myWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat: @"window.scrollTo(0,%i);",414*self.initialScrollPosition]];

Everything going right, but on problem that I am facing is as follows.

Whenever User/I tap on the status bar of iPhone, WebView Bydefault scrolls to top. This should not be done.

Is it possible to prevent inbuilt functionality ?

I know one of the option is as follows.

((UIScrollView *)[[myWebView valueForKey:@"_internal"] valueForKey:@"scroller"]).scrollsToTop = NO;

But is it valid to do ?

sagarkothari
  • 24,520
  • 50
  • 165
  • 235

2 Answers2

4

You can add a very tiny UIScrollView in the window. Then tapping the status bar won't scroll the web view to top.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
1

A more straightforward way to do this would be to set the scrollsToTop property of the UIScrollView in the WebView to NO.

for(UIView *view in [myWebView subviews]) {
    if([view isKindOfClass:([UIScrollView class])]) {
        [(UIScrollView *)view setScrollsToTop:NO];
    }
}

I have tested this on iOS 4.0 and 4.3 (iOS 5 seems to not need this).

adapted from this.

Community
  • 1
  • 1
Tommy
  • 594
  • 5
  • 8