2009-09-16 47 views
3

我使用一個UIWebView(裝有一個HTML文件),我想偏移的滾動位置和偏移量應保持不變,如果我改變文字size.I要存儲先前滾動位置,即使如果我更改文字大小。這個怎麼做????UIWebView的偏移設置問題

回答

2

我總滾動高度另一種方法,我發現在http://www.alexnking.com/2008/10/14/going-back-to-the-last-place-you-were-in-a-uiwebview/,當你想SC可能有很大的幫助手動滾動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; 

    } 

} 

希望它可以幫助!

+2

我想你應該歸功於來源:http://www.alexnking.com/2008/10/14/going-back-to-the-last-place-you-were-in-a-uiwebview/ – 2010-09-14 17:10:31

+0

對不起湯米,我會記住這一點在下一次 – RVN 2010-09-14 18:24:08

3

獲取當前偏移使用:

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

對於設置頁面偏移使用:

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

用於獲取頁面

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