2009-09-16 71 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];