質問
UIWebView(HTMLファイルでロードされた)を使用していますが、スクロール位置のオフセットが必要であり、テキストサイズを変更した場合、オフセットは一定のままでなければなりません。以前にスクロールした位置を変更しても保存したい文字サイズ 。これを行う方法????
解決
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 pageYOffset = [[webView stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue];
ページのオフセットを設定する場合:
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.scrollTop = %d", 100]];
ページの合計スクロール可能高さを取得するため
int scrollHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"] intValue];
所属していません StackOverflow