简体   繁体   English

iphone UIWebView存储当前滚动位置?

[英]iphone UIWebView store the current scroll position?

i want to store the current scroll position of a webView and when i launch the application again . 我想存储webView的当前滚动位置,以及当我再次启动应用程序时。 i want to restore that scroll position again .. 我想再次恢复滚动位置..

How to do that ??? 怎么做 ???

UIWebView is not a subclass of UIScrollView so you can't do it the "normal" way. UIWebView不是UIScrollView的子类,因此您无法以“正常”方式执行此操作。

Looks like this javascript solution will do what you need: http://shortdivision.wordpress.com/2008/10/14/going-back-to-the-last-place-you-were-in-a-uiwebview/ 看起来这个javascript解决方案将满足您的需求: http//shortdivision.wordpress.com/2008/10/14/going-back-to-the-last-place-you-were-in-a-uiwebview/

Since iOS5 UIWebView has scrollView property. 由于iOS5 UIWebView具有scrollView属性。 You can use variable of type CGPoint defined like this: 您可以使用如下定义的CGPoint类型的变量:

CGPoint _scrollPosition;

Then initialize it: 然后初始化它:

_scrollPosition = CGPointMake(0, 0);

Whereever you need to store the current position use: 无论您需要存储当前位置,请使用:

_scrollPosition = _webView.scrollView.contentOffset;

Implement UIWebViewDelegate protocol and add code to restore the position inside -(void)webViewDidFinishLoad:(UIWebView *)webView : 实现UIWebViewDelegate协议并添加代码以恢复其中的位置-(void)webViewDidFinishLoad:(UIWebView *)webView

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    // .... your other code here ...

    if (0 < _scrollPosition.y) {
        _webView.scrollView.contentOffset = _scrollPosition;
        _scrollPosition = CGPointMake(0, 0);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM