How to set content offset and content size in a UIWebView

前端 未结 5 2210
抹茶落季
抹茶落季 2020-12-05 14:48

I have had to change a UIScrollView into a UIWebView due to formatting reasons.

There is a problem though that I need to be able to get the

相关标签:
5条回答
  • 2020-12-05 15:24

    The best way I found to sort the problem was:

    1. Create a UIWebView
    2. Put it in a UIScrollView
    3. Add the HTML into it using "loadHTMLString"
    4. Using the "- (void)webViewDidFinishLoad:(UIWebView *)webView {" delegate method I detect the size of the UIWebView

    5. Set the frame of the UIWebView to the content size.

    6. Remove interaction from the UIWebView
    7. Set the Content size of the UIScrollView to the content size.

    Then used the functionality of the UIScrollView instead of the UIWebView.

    Thanks -JM

    0 讨论(0)
  • 2020-12-05 15:24

    Use JavaScript.

    I think that the next line should do the magic:

    [webView stringByEvaluatingJavaScriptFromString:@"window.scroll(0,0)"];
    
    0 讨论(0)
  • 2020-12-05 15:32

    This problem can be solved quite easily:

    [[webView scrollView] setContentInset:UIEdgeInsetsMake(64, 0, 44, 0)];
    

    Remember that UIEdgeInset is different from CGRect!
    A guide on UIEdgeInset can be found in the iOS Developer Library.

    0 讨论(0)
  • 2020-12-05 15:32

    Once you get the UIScrollView that is inside the UIWebView, it will be easy to change ContentOffset and contentSize. Information here on how to get the scrollView : ScrollOffset in UIWebView?

    0 讨论(0)
  • 2020-12-05 15:42

    Now you can ask for the contentSize property of the scrollView property of a UIWebView. This enables you to also change the content size of that web view, using CGSizeMake (width, height).

    0 讨论(0)
提交回复
热议问题