I have a html string, and I load the html string to UIWebView, and the user may scroll down the UIWebView . When sometime the html string I changed ,so I call the UIWebView load the new html string. But when I load it ,the UIWebView scroll to top.The difference between the new string and the old string may be little ,so I don't what the UIWebView scroll to top. It should be the previous position what the user scrolled it. How can I do that ?
Asked
Active
Viewed 1,098 times
0
-
the content of the uiwebview changed and reloaded.. one of solutions that i see is to check the previous offset of the uiwebview and store it..when the new html string is loaded scroll your web view to the same offset..if you can the difference between the two html contents try to add it to the old offset. – Mejdi Lassidi May 02 '15 at 04:14
-
I search a lot of problems about setting contentoffset, it did not seem to work... – leizh00701 May 02 '15 at 04:25
-
try this : http://stackoverflow.com/questions/3525256/how-to-set-content-offset-and-content-size-in-a-uiwebview – Mejdi Lassidi May 02 '15 at 04:35
1 Answers
0
the content of the uiwebview changed and reloaded.. one of solutions that i see is to check the previous offset of the uiwebview and store it..when the new html string is loaded scroll your web view to the same offset. if you can get the difference between the two html contents try to add it to the old offset.
you can use javascript to get the offset :
int scrollPosition = [[webView stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue];
and also to scroll to the new offset using :
[webView stringByEvaluatingJavaScriptFromString:@"window.scroll(0,newOffset)"];

Mejdi Lassidi
- 999
- 10
- 22
-
I use [webView stringByEvaluatingJavaScriptFromString:@"window.scroll(0,newOffset)"]; to set the contentoffset .and then I print out the newoffset. it is correct. But the uiwebview was show in the top? It did not scroll... – leizh00701 May 02 '15 at 05:02
-
try to make it for the the first webview's content..for me i have tested and it works fine. – Mejdi Lassidi May 02 '15 at 05:04
-
You did't use the scroll position. This is what you should use insted of the second line of code: [_contentWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.scroll(0,%d)" , scrollPosition]]; – MCMatan Aug 04 '16 at 12:03