iphone UIWebView store the current scroll position?

不羁岁月 提交于 2019-12-09 06:22:42

问题


i want to store the current scroll position of a webView and when i launch the application again . i want to restore that scroll position again ..

How to do that ???


回答1:


UIWebView is not a subclass of UIScrollView so you can't do it the "normal" way.

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/




回答2:


Since iOS5 UIWebView has scrollView property. You can use variable of type CGPoint defined like this:

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:

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

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


来源:https://stackoverflow.com/questions/1394766/iphone-uiwebview-store-the-current-scroll-position

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!