how to get proper UIWebView content height?

≡放荡痞女 提交于 2019-12-08 06:43:09

问题


I am add UIWebView in View and That view in ScrollView.

I want to get frame of the UIWebView Content. So can I set the frame to view and scrollview?

I know we can get the height by using javascript. As mention below:

float height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue];

But when I am rotating the view and recalculate the height of the UIWebView content in didRotateFromInterfaceOrientation::(UIInterfaceOrientation)fromInterfaceOrientation method, then content height was increased.

So please suggest me how can I get the proper height when i am rotate the orientation?

Thanks.


回答1:


Adopt this method:

- (void) webViewDidFinishLoad:(UIWebView *)webView{
    CGRect frame = wvContent.frame;
    CGSize fittingSize = [wvContent sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    wvContent.frame = frame;
}

Then call this method everytime device rotating:

- (void) reloadContent
{
    CGRect frame = wvContent.frame;
    frame.size.height = 1;
    frame.size.width = wvContent.frame.size.width; // you should change webview's width here.
    wvContent.frame = frame;

    [wvContent loadHTMLString:yourHTML baseURL:nil]; // reload webview
}

Hope that help :)




回答2:


Make sure that you are retrieving height when all the content is loaded.

You can add a method to calculate height in UIWebView's delegates method

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    float height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue];
}


来源:https://stackoverflow.com/questions/12087002/how-to-get-proper-uiwebview-content-height

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