【发布时间】:2009-09-16 07:21:11
【问题描述】:
我正在使用 UIWebView(加载了 HTML 文件),我希望滚动位置的偏移量,如果我更改文本大小,偏移量应该保持不变。我想存储以前滚动的位置,即使我更改了字体大小 。这个怎么办????
【问题讨论】:
我正在使用 UIWebView(加载了 HTML 文件),我希望滚动位置的偏移量,如果我更改文本大小,偏移量应该保持不变。我想存储以前滚动的位置,即使我更改了字体大小 。这个怎么办????
【问题讨论】:
获取当前的偏移量使用:
int pageYOffset = [[webView stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue];
用于设置页面偏移使用:
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.scrollTop = %d", 100]];
【讨论】:
我在http://www.alexnking.com/2008/10/14/going-back-to-the-last-place-you-were-in-a-uiwebview/ 找到了另一种方法,当您想要手动滚动 UIWebView 时,它可能会很有帮助。
获取当前滚动位置(垂直):
[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;
}
}
【讨论】:
用于获取页面的总滚动高度
int scrollHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"] intValue];
【讨论】: