Question

I am using a UIWebView(loaded with a HTML file) and i want the offset of the scrolled position and the offset should remain constant if i change the text size.I want to store the previously scrolled position even if i change the text Size . HOw to do this????

Was it helpful?

Solution

I have another method which I found at http://www.alexnking.com/2008/10/14/going-back-to-the-last-place-you-were-in-a-uiwebview/, which might be of great help when you want to scroll a UIWebView manually.

To get the current scroll position (vertical):

[myWebView stringByEvaluatingJavaScriptFromString: @"scrollY"];

// Application is terminating.

- (void)applicationWillTerminate:(UIApplication *)application {

  [[NSUserDefaults standardUserDefaults] setInteger:
    [[myWebView stringByEvaluatingJavaScriptFromString: @"scrollY"]
      intValue] forKey: @"currentScroll"];
}

// Application is loading.

- (void)applicationDidFinishLaunching:(UIApplication *)application {

 initialScrollPosition = [[NSUserDefaults standardUserDefaults]
                                integerForKey: @"currentScroll"];
}

// WebView is finished loading

- (void)webViewDidFinishLoad:(UIWebView *)webView {

  // Check if we need to scroll this somewhere.

  if (initialScrollPosition != 0) {

    // Scroll to the position.

    [passageView stringByEvaluatingJavaScriptFromString:
      [NSString stringWithFormat: @"window.scrollTo(0, %d);",
      self.initialScrollPosition]]; 

    // Set the initial scroll value to zero so we don't try

    // to scroll to it again if the user navigates to another page.

    self.initialScrollPosition = 0;

  }

}

OTHER TIPS

For getting the current Offset use :

int pageYOffset = [[webView stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue];

For setting the page Offset use:

    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.scrollTop = %d", 100]];

For getting the total scrollable height of the page

int scrollHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"] intValue];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top