iphone UIWebView store the current scroll position?

后端 未结 2 1888
忘掉有多难
忘掉有多难 2021-02-10 11:12

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 ???

2条回答
  •  别那么骄傲
    2021-02-10 11:42

    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);
        }
    }
    

提交回复
热议问题