views:

782

answers:

4

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????

+2  A: 

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]];
Biranchi
+1  A: 

For getting the total scrollable height of the page

int scrollHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"] intValue];
Biranchi
+1  A: 

Can you please let me know where to write the above code .Basically,I want to know where to detect the scrolling of webview.

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

Thanks In advance,

cooliphonedeveloper
A: 

I have another method which i found , might be of great help, when u want to scroll 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;

    }

}

Hope it helps !!!

RVN
I think you ought to credit the source: http://www.alexnking.com/2008/10/14/going-back-to-the-last-place-you-were-in-a-uiwebview/
Tommy Herbert
Sorry Tommy , i will keep that in mind next time
RVN