Problem setting UIwebView offset

I am using a UIWebView (loaded with an HTML file) and I need the offset of the scroll position, and the offset should remain constant if I resize the text. I want to keep the previously scrolled position, even if I resize the text. HOw to do it ????

+3
source share
3 answers

I have another method that I found at http://www.alexnking.com/2008/10/14/going-back-to-the-last-place-you-were-in-a-uiwebview/ which can be of great help when you want to scroll through the 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;

  }

}
+2
source

:

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

:

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

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

Source: https://habr.com/ru/post/1717807/


All Articles